SIGN IN SIGN UP

Debouncing (#455)

Workflows can now be debounced. Debouncing delays workflow execution
until some time has passed since the workflow has last been called. This
is useful for preventing wasted work when a workflow may be triggered
multiple times in quick succession.
For example, if a user is editing a form, we can debounce every change
to the form and perform a synchronization workflow only after they
haven't edited the form for a certain period of time.

### Debouncer.create

```python
Debouncer.create(
    workflow: Callable[P, R],
    *,
    debounce_key: str,
    debounce_timeout_sec: Optional[float] = None,
    queue: Optional[Queue] = None,
) -> Debouncer[P, R]
```

**Parameters:**
- `workflow`: The workflow to debounce.
- `debounce_key`: The **unique** debounce key for this debouncer. Used
to group workflows that will be debounced.
- `debounce_timeout_sec`: After this time elapses since the first time a
workflow is submitted from this debouncer, the workflow is started
regardless of the debounce period.
- `queue`: When starting a workflow after debouncing, enqueue it on this
queue instead of executing it directly.

### debounce

```python
debouncer.debounce(
    debounce_period_sec: float,
    *args: P.args,
    **kwargs: P.kwargs,
) -> WorkflowHandle[R]
```

Submit a workflow for execution but delay it by `debounce_period_sec`.
Returns a handle to the workflow.
The workflow may be debounced again, which further delays its execution
(up to `debounce_timeout_sec`).
When the workflow eventually executes, it uses the **last** set of
inputs passed into `debounce`.

After the workflow begins execution, the next call to `debounce` starts
the debouncing process again for a new workflow execution.

**Example Syntax**:

```python
@DBOS.workflow()
def process_input(user_input):
    ...

# Each time a user submits a new input, debounce the process_input workflow.
# The workflow will wait until 60 seconds after the user stops submitting new inputs,
# then process the last input submitted.
def on_user_input_submit(user_id, user_input):
    debounce_period_sec = 60
    debouncer = Debouncer.create(process_input, debounce_key=user_id)
    debouncer.debounce(debounce_period_sec, user_input)
```
P
Peter Kraft committed
a5e973f53acda03501eccb99f497dee474bb0eac
Parent: f198a7c
Committed by GitHub <noreply@github.com> on 9/10/2025, 11:20:33 PM