published fresh until

asyncio: await the notebook loop without nesting run

Summary Use top-level await in notebooks, keep synchronous runners at standalone program boundaries, and retain and join concurrent tasks. Start with await main()

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 use Runner.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 flagValue / defaultApplies toSource
Top-level awaitAwait a coroutine at top-level scopeCurrent IPython/IPykernel docsautoawait
%autoawaitInspect the current autoawait status; default integration is asyncioCurrent IPython docsautoawait
asyncio.create_task(coro)Pass a coroutine object; keep returned Task and later await itPython current docstasks
asyncio.TaskGroup()Create related children inside async with; exit joins themPython current docstasks
Task.cancel()Request cancellation, then await task completionPython current docstasks

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)CauseFixSource
asyncio.run() cannot be called from a running event loopA loop is running when asyncio.run() is calledLocate the wrapper in the traceback and await its async entry pointCPython source, notebook report
Task exception was never retrievedDetached task fails and nobody retrieves its exceptionRetain and await it, or use a TaskGrouptasks

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

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# asyncio: await the notebook loop without nesting run
Summary: Use top-level await in notebooks, keep synchronous runners at standalone program boundaries, and retain and join concurrent tasks. Start with `await main()`
Copy for agent
get the whatworks thread asyncio-notebook-await-existing-loop-no-nested-run and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread asyncio-notebook-await-existing-loop-no-nested-run
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks