Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions examples/workflow/async_activities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# -*- coding: utf-8 -*-
# Copyright 2026 The Dapr Authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Async activities running alongside a sync one in a fan-out/fan-in workflow.

Each async activity simulates an I/O-bound call: it takes a payload, awaits a fixed
delay (standing in for a network round-trip), and returns a result payload. The async
instances run concurrently on the worker's event loop; a final sync activity aggregates
the results. Fan-out width, input/output payload sizes, and the delay are configurable
via environment variables.

Run with:

dapr run --app-id async-activities --app-protocol grpc --dapr-grpc-port 50001 \\
-- python async_activities.py
"""

from __future__ import annotations

import asyncio
import os
import random
import string
from time import sleep

import dapr.ext.workflow as wf
from pydantic import BaseModel

FAN_OUT = int(os.environ.get('WORKFLOW_FAN_OUT', '5'))
INPUT_BYTES = int(os.environ.get('WORKFLOW_INPUT_BYTES', '2048'))
OUTPUT_BYTES = int(os.environ.get('WORKFLOW_OUTPUT_BYTES', '1024'))
IO_SECONDS = float(os.environ.get('WORKFLOW_IO_SECONDS', '1.0'))

wfr = wf.WorkflowRuntime()


def _random_digits(n: int) -> str:
return ''.join(random.choices(string.digits, k=n))


class Payload(BaseModel):
index: int
data: str


@wfr.workflow(name='fan_out_fan_in_workflow')
def fan_out_fan_in_workflow(ctx: wf.DaprWorkflowContext, payloads: list[dict]):
tasks = [ctx.call_activity(process_payload, input=p) for p in payloads]
results = yield wf.when_all(tasks)
summary = yield ctx.call_activity(summarize, input=results)
return summary


@wfr.activity(name='process_payload')
async def process_payload(ctx: wf.WorkflowActivityContext, payload: Payload) -> str:
"""Async activity: simulate an I/O-bound call. Instances run concurrently on the loop."""
await asyncio.sleep(IO_SECONDS)
result = _random_digits(OUTPUT_BYTES)
print(
f'[async] payload {payload.index}: {len(payload.data)}B in -> {len(result)}B out',
flush=True,
)
return result


@wfr.activity(name='summarize')
def summarize(ctx: wf.WorkflowActivityContext, results: list[str]) -> str:
"""Sync activity: aggregate the fan-out results on the thread pool."""
summary = f'{len(results)} results, {sum(len(r) for r in results)} bytes'
print(f'[sync] {summary}', flush=True)
return summary


def main() -> None:
payloads = [
Payload(index=i, data=_random_digits(INPUT_BYTES)).model_dump() for i in range(FAN_OUT)
]

wfr.start()
sleep(5) # wait for workflow runtime to start

wf_client = wf.DaprWorkflowClient()
instance_id = wf_client.schedule_new_workflow(workflow=fan_out_fan_in_workflow, input=payloads)
print(f'Workflow started. Instance ID: {instance_id}')

state = wf_client.wait_for_workflow_completion(instance_id, timeout_in_seconds=60)
assert state is not None
print(f'Workflow completed! Status: {state.runtime_status.name}')
print(f'Workflow result: {state.serialized_output.strip(chr(34))}')

wfr.shutdown()


if __name__ == '__main__':
main()
23 changes: 22 additions & 1 deletion ext/dapr-ext-workflow/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,26 @@ The entry point for registration and lifecycle:

Internally wraps user functions: workflow functions get a `DaprWorkflowContext`, activity functions get a `WorkflowActivityContext`. Tracks registration state via `_workflow_registered` / `_activity_registered` attributes on functions to prevent double registration.

#### Sync and async activities

Activities can be either `def my_activity(ctx, inp)` or `async def my_activity(ctx, inp)`. At registration, `_make_activity_wrapper` calls `_is_async_callable(fn)` to detect async-ness. That helper unwraps `functools.partial`, `@functools.wraps` chains, and callable-class `__call__` so common decorator patterns route correctly. The wrapper is built `async def` or `def` to match, then stored in the registry.

At dispatch time (the gRPC stream loop in `_durabletask/worker.py`), `is_async_callable(activity_fn)` on the wrapper selects between two handlers.

- **Async activities** go through `_execute_activity_async`, then `_ActivityExecutor.execute_async`, which awaits `fn(...)` directly on the event loop. The gRPC response is delivered via `loop.run_in_executor(self._async_worker_manager.thread_pool, stub.CompleteActivityTask, ...)` — the same pool sync activities use, sized by `maximum_thread_pool_workers`.
- **Sync activities** go through `_execute_activity`, dispatched to the thread pool by `_AsyncWorkerManager._run_func`. The activity runs on a worker thread, and the response is delivered from the same thread.

Workflow (orchestrator) functions must remain generators (`def` with `yield`). They cannot be `async def` because durabletask's deterministic replay depends on synchronous generator semantics. Only activities support async.

**Decorator ordering gotcha.** Stacking `@wfr.activity` over `@alternate_name(...)` over `async def` works because `@alternate_name` now emits an `async def innerfn` when the wrapped function is async. A user-written decorator that wraps an async function in a sync `def` (without `@functools.wraps` exposing `__wrapped__`) defeats `_is_async_callable`, routes the activity to the sync path, and produces an un-awaited coroutine. Such decorators should use `@functools.wraps(fn)` so the unwrap walks through them.

**`maximum_thread_pool_workers` covers both paths.** This knob sizes the worker thread pool used for sync-activity bodies and for async-activity gRPC response sends. Mixed workloads with long-running sync activities can starve async response delivery (and vice versa) since they share the pool — size to the sum of peak sync activity concurrency and peak in-flight async response sends.

**Concurrency sizing and load characterization.** See `docs/concurrency.md` for sizing recommendations (`maximum_concurrent_activity_work_items`, `maximum_thread_pool_workers`) and an async-vs-sync decision tree. `tests/durabletask/test_async_dispatch_regression.py` (marked `perf`) guards the core invariant: a batch of async activities overlaps on the event loop instead of serializing through the thread pool.

**grpc.aio poller log noise.** The async client can emit benign `BlockingIOError: [Errno 11]` ERROR lines from `grpc.aio`'s `PollerCompletionQueue` under load. It is harmless and retried. `get_grpc_aio_channel` installs an internal `asyncio`-logger filter (`_silence_grpc_aio_poller_noise`) that drops only those records, so the SDK suppresses it automatically with no user action.


### DaprWorkflowClient (`dapr_workflow_client.py`)

Client for workflow lifecycle management:
Expand Down Expand Up @@ -163,7 +183,7 @@ Retry configuration for activities and child workflows:
1. **Registration**: User decorates functions with `@wfr.workflow` / `@wfr.activity`. The runtime wraps them and stores them in the durabletask worker's registry.
2. **Startup**: `wfr.start()` opens a gRPC stream to the Dapr sidecar. The worker polls for work items.
3. **Scheduling**: Client calls `schedule_new_workflow(fn, input=...)`. The function's name (or `_dapr_alternate_name`) is sent to the backend.
4. **Execution**: The durabletask engine dispatches work items. Workflow functions are Python **generators** that `yield` tasks (activity calls, timers, child workflows). The engine records history; on replay, yielded tasks return cached results without re-executing.
4. **Execution**: The durabletask engine dispatches work items. Workflow functions are Python **generators** that `yield` tasks (activity calls, timers, child workflows). Activity functions are either sync (dispatched to the worker's thread pool) or `async def` (awaited directly on the worker's event loop). The engine records history; on replay, yielded tasks return cached results without re-executing.
5. **Determinism**: Workflows must be deterministic — no random, no wall-clock time, no I/O. Use `ctx.current_utc_datetime` instead of `datetime.now()`. Use `ctx.is_replaying` to guard side effects like logging.
6. **Completion**: Client polls via `wait_for_workflow_completion()` or `get_workflow_state()`.

Expand Down Expand Up @@ -191,6 +211,7 @@ Two example directories exercise workflows:
- `cross-app1.py`, `cross-app2.py`, `cross-app3.py` — cross-app calls
- `versioning.py` — workflow versioning with `is_patched()`
- `simple_aio_client.py` — async client variant
- `async_activities.py` — `async def` activities (fan-out/fan-in with simulated I/O, configurable payload sizes)

## Testing

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,32 @@ def __init__(
else:
interceptors = None

channel = get_grpc_aio_channel(
host_address=host_address,
secure_channel=secure_channel,
interceptors=interceptors,
options=channel_options,
)
self._channel = channel
self._stub = stubs.TaskHubSidecarServiceStub(channel)
self._host_address = host_address
self._secure_channel = secure_channel
self._interceptors = interceptors
self._channel_options = channel_options
self._channel: grpc.aio.Channel | None = None
self._stub: stubs.TaskHubSidecarServiceStub | None = None
self._logger = shared.get_logger('client', log_handler, log_formatter)

def _get_stub(self) -> stubs.TaskHubSidecarServiceStub:
"""Lazily create the channel and stub on first use.

Async grpc binds a channel to the loop active at creation, deferring it avoids binding to the wrong loop.
"""
if self._stub is None:
self._channel = get_grpc_aio_channel(
host_address=self._host_address,
secure_channel=self._secure_channel,
interceptors=self._interceptors,
options=self._channel_options,
)
self._stub = stubs.TaskHubSidecarServiceStub(self._channel)
return self._stub

async def aclose(self):
await self._channel.close()
if self._channel is not None:
await self._channel.close()

async def __aenter__(self):
return self
Expand Down Expand Up @@ -112,14 +126,14 @@ async def schedule_new_orchestration(
)

self._logger.info(f"Starting new '{name}' instance with ID = '{req.instanceId}'.")
res: pb.CreateInstanceResponse = await self._stub.StartInstance(req)
res: pb.CreateInstanceResponse = await self._get_stub().StartInstance(req)
return res.instanceId

async def get_orchestration_state(
self, instance_id: str, *, fetch_payloads: bool = True
) -> Optional[WorkflowState]:
req = pb.GetInstanceRequest(instanceId=instance_id, getInputsAndOutputs=fetch_payloads)
res: pb.GetInstanceResponse = await self._stub.GetInstance(req)
res: pb.GetInstanceResponse = await self._get_stub().GetInstance(req)
return new_orchestration_state(req.instanceId, res)

async def wait_for_orchestration_start(
Expand All @@ -131,7 +145,7 @@ async def wait_for_orchestration_start(
)

async def _call(grpc_timeout):
res: pb.GetInstanceResponse = await self._stub.WaitForInstanceStart(
res: pb.GetInstanceResponse = await self._get_stub().WaitForInstanceStart(
req, timeout=grpc_timeout
)
return new_orchestration_state(req.instanceId, res)
Expand All @@ -150,7 +164,7 @@ async def wait_for_orchestration_completion(
)

async def _call(grpc_timeout):
res: pb.GetInstanceResponse = await self._stub.WaitForInstanceCompletion(
res: pb.GetInstanceResponse = await self._get_stub().WaitForInstanceCompletion(
req, timeout=grpc_timeout
)
state = new_orchestration_state(req.instanceId, res)
Expand Down Expand Up @@ -261,7 +275,7 @@ async def raise_orchestration_event(
)

self._logger.info(f"Raising event '{event_name}' for instance '{instance_id}'.")
await self._stub.RaiseEvent(req)
await self._get_stub().RaiseEvent(req)

async def terminate_orchestration(
self, instance_id: str, *, output: Optional[Any] = None, recursive: bool = True
Expand All @@ -273,19 +287,19 @@ async def terminate_orchestration(
)

self._logger.info(f"Terminating instance '{instance_id}'.")
await self._stub.TerminateInstance(req)
await self._get_stub().TerminateInstance(req)

async def suspend_orchestration(self, instance_id: str):
req = pb.SuspendRequest(instanceId=instance_id)
self._logger.info(f"Suspending instance '{instance_id}'.")
await self._stub.SuspendInstance(req)
await self._get_stub().SuspendInstance(req)

async def resume_orchestration(self, instance_id: str):
req = pb.ResumeRequest(instanceId=instance_id)
self._logger.info(f"Resuming instance '{instance_id}'.")
await self._stub.ResumeInstance(req)
await self._get_stub().ResumeInstance(req)

async def purge_orchestration(self, instance_id: str, recursive: bool = True):
req = pb.PurgeInstancesRequest(instanceId=instance_id, recursive=recursive)
self._logger.info(f"Purging instance '{instance_id}'.")
await self._stub.PurgeInstances(req)
await self._get_stub().PurgeInstances(req)
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
from typing import Optional, Sequence, Union

import grpc
Expand All @@ -27,6 +28,30 @@
grpc_aio.StreamStreamClientInterceptor,
]

_POLLER_NOISE_MARKER = 'PollerCompletionQueue._handle_events'


class _GrpcAioPollerNoiseFilter(logging.Filter):
"""Drops the harmless grpc.aio poller BlockingIOError (EAGAIN) records.

The poller does a non-blocking read on its wake-up fd and can get EAGAIN, which
asyncio logs at ERROR even though the read is retried and nothing is lost.
"""

def filter(self, record: logging.LogRecord) -> bool:
exc = record.exc_info[1] if record.exc_info else None
is_poller_noise = isinstance(exc, BlockingIOError) and (
_POLLER_NOISE_MARKER in record.getMessage()
)
return not is_poller_noise


def _silence_grpc_aio_poller_noise() -> None:
Comment thread
seherv marked this conversation as resolved.
"""Install the poller-noise filter on the asyncio logger if not already present."""
asyncio_logger = logging.getLogger('asyncio')
if not any(isinstance(f, _GrpcAioPollerNoiseFilter) for f in asyncio_logger.filters):
asyncio_logger.addFilter(_GrpcAioPollerNoiseFilter())


def get_grpc_aio_channel(
host_address: Optional[str],
Expand All @@ -42,6 +67,8 @@ def get_grpc_aio_channel(
interceptors: Optional sequence of client interceptors to apply to the channel.
options: Optional sequence of gRPC channel options as (key, value) tuples. Keys defined in https://grpc.github.io/grpc/core/group__grpc__arg__keys.html
"""
_silence_grpc_aio_poller_noise()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this only on the asyncio side of things?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, grpc.aio spams the error logs when their client is used from multiple event loops, and that was the case for FastAPI applications using this SDK. Nothing was actually an error but the logs got extremely noisy in Linux.

It got fixed on their 1.80.0 release, as soon as we update to that dep (in a separate PR ofc) we can delete this


if host_address is None:
host_address = get_default_host_address()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# limitations under the License.

import dataclasses
import functools
import inspect
import json
import logging
import os
Expand All @@ -19,6 +21,28 @@
import grpc
from dapr.ext.workflow import _model_protocol


def is_async_callable(fn: Any) -> bool:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a test somewhere for this addition?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, it's at tests/test_async_activity_registration.py

"""Return True if ``fn`` is async. Catches ``functools.partial`` of coroutines,
sync decorators that wrap async functions, and callable instances with ``async __call__``.
"""
candidate = fn
while isinstance(candidate, functools.partial):
candidate = candidate.func
if callable(candidate):
try:
candidate = inspect.unwrap(candidate)
except ValueError:
# Cyclic ``__wrapped__`` chain from a malformed decorator. Fall back to the
# outermost callable; misclassification is preferable to crashing dispatch.
pass
Comment thread
seherv marked this conversation as resolved.
Comment on lines +35 to +38

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we warn log here?

if inspect.iscoroutinefunction(candidate):
return True
if not inspect.isfunction(candidate) and hasattr(candidate, '__call__'):
return inspect.iscoroutinefunction(candidate.__call__)
return False
Comment thread
seherv marked this conversation as resolved.


ClientInterceptor = Union[
grpc.UnaryUnaryClientInterceptor,
grpc.UnaryStreamClientInterceptor,
Expand Down
Loading
Loading