What this thread does
Do first: replace the notebook entry point with await main() and follow the traceback to any synchronous wrapper still calling asyncio.run().
Short answer
As of November 2026, Python 3.15.2, released October 25 is the runtime reference. IPykernel keeps an event loop running; a notebook cell can await the coroutine on that loop (IPython autoawait). A runner cannot be called when another asyncio event loop is running in the same thread. It does not mean the coroutine needs another runner (runner API).
Choose / don't choose
- Direct await: choose
await main()when the caller needs the result before continuing; don't create a task solely to await it immediately. - Concurrent task: choose
task = asyncio.create_task(coro)when independent work must overlap; pass a coroutine object and keep the task reference (task API). - Standalone script: choose
asyncio.run(main())as the entry-point example; don't useRunner.run()to bypass an already-running loop (CPython source).
Not for: unawaited coroutine warnings or blocking FastAPI handlers; see python-coroutine-never-awaited-and-fastapi-async-def-blocking.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
Top-level await | Await a coroutine at top-level scope | Current IPython/IPykernel docs | autoawait |
%autoawait | Inspect the current autoawait status; default integration is asyncio | Current IPython docs | autoawait |
asyncio.create_task(coro) | Pass a coroutine object; keep returned Task and later await it | Python current docs | tasks |
asyncio.TaskGroup() | Create related children inside async with; exit joins them | Python current docs | tasks |
Task.cancel() | Request cancellation, then await task completion | Python current docs | tasks |
Order of operations
1. Read the traceback from the notebook cell down to the first application/library call to asyncio.run. Replace that synchronous wrapper with its coroutine API and propagate await through callers. A runner cannot be called when another asyncio event loop is running in the same thread (runner API). 2. For concurrency, retain task references to avoid tasks disappearing mid-execution. A TaskGroup waits for a group of related tasks (tasks). 3. On early exit, request cancellation of the task, then await the cancelled task before releasing shared resources. Propagate cancellation after cleanup. Never cancel unrelated kernel tasks (tasks).
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
asyncio.run() cannot be called from a running event loop | A loop is running when asyncio.run() is called | Locate the wrapper in the traceback and await its async entry point | CPython source, notebook report |
Task exception was never retrieved | Detached task fails and nobody retrieves its exception | Retain and await it, or use a TaskGroup | tasks |
Diagnostic: asyncio.run() cannot be called from a running event loop
An async notebook entry point fixes only wrappers you actually replace. A synchronous library method that internally calls a runner needs that library's async API, not another outer runner.
Missing
- A particular library's async equivalent cannot be named without that library and call stack; no loop-patching dependency is required for the direct-await branch.
Checked
2026-11-12; Python release, runner/task references, IPython autoawait, CPython runner source and the dated notebook report read. No notebook or application commands executed.
Started from a post by @Python Software Foundation, packaged by the editor. Original post