alex toney
09/12/2025, 3:26 AMMarvin
09/12/2025, 3:27 AMMarvin
09/12/2025, 3:30 AMfrom prefect.utilities.asyncutils import run_coro_as_sync
def do_something_sync():
return run_coro_as_sync(async_fn(...))
API reference (source): prefect.utilities.asyncutils
- Flows and tasks across sync/async:
- Sync flow with async task:
from prefect import flow, task
@task
async def a_task(x):
return x + 1
@flow
def my_flow():
fut = a_task.submit(41) # submit bridges sync->async
return fut.result()
- Fully async:
from prefect import flow, task
@task
async def a_task(x):
return x + 1
@flow
async def my_async_flow():
return await a_task(41)
- In notebooks (Jupyter/IPython):
- IPython already runs an event loop and supports top-level await. Prefer:
await my_async_flow()
- If your flow is sync, just call it normally: my_flow()
- Avoid asyncio.run(...) and avoid patching with nest_asyncio.
- Tests:
- For async flows/tasks, mark tests with pytest.mark.asyncio and await them.
- For sync tests calling async helpers, use run_coro_as_sync instead of asyncio.run.
- Deployments and workers (Kubernetes, Docker, etc.):
- Workers manage their own event loops and execution contexts. Patching with nest_asyncio inside your code will not consistently propagate and may interfere with Prefect’s task scheduling and cancellation semantics. Avoid it.
Common “don’ts” in Prefect
- Don’t call asyncio.run inside a flow or task.
- Don’t create/close event loops manually.
- Don’t rely on nest_asyncio to “fix” nested loop errors—use Prefect’s utilities and patterns instead.
Background/related discussion
- The Prefect team’s direction has been to provide first-class sync/async bridging without requiring users to manage loops manually. See the historical (2.x-era) discussion around sync-friendly client usage: Allow Prefect client to be used from synchronous contexts. In 3.x, many APIs are made sync-compatible and the async utilities above are the supported path.
If you’re hitting a specific place where you felt nest_asyncio was needed (e.g., a notebook pattern, testing setup, or calling an async library inside a sync flow), share a snippet and I’ll suggest an idiomatic Prefect 3.x alternative.Nate
09/12/2025, 2:39 PMNate
09/12/2025, 2:40 PMnot recommended or tested by the Prefect teambut i support this message
alex toney
09/15/2025, 8:17 PMNate
09/15/2025, 8:17 PM