nest_asyncio

Patch asyncio to allow nested event loops in Jupyter, web servers, and GUIs

Library
PyPI
v1.6.0
753stars
BSD-2-Clause

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
40/100Fair
Development Activity0
Maintenance20
Community52
Maturity60
Momentum28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
65/100Good
Architecture55
Code Quality62
Innovation58
Learning Curve85

nest_asyncio is a tiny, single-purpose Python library that patches the standard library’s asyncio module to allow its event loop to be nested. By design, asyncio refuses to let you call run_until_complete or asyncio.run from inside a coroutine that is already running on the same loop, raising RuntimeError: This event loop is already running. That restriction becomes a real obstacle in environments like Jupyter notebooks, IPython kernels, and GUI toolkits that already own a running event loop, since any code path that itself wants to drive async work to completion hits the wall immediately.

Calling nest_asyncio.apply() monkey-patches asyncio’s Task/Future classes, the event loop policy, and the loop’s run_forever/run_until_complete/_run_once internals so that reentrant calls are handled safely instead of raising. The patch is applied once per loop (guarded by a _nest_patched flag) and optionally makes Tornado’s Future class asyncio-compatible when Tornado is already imported. It requires no configuration beyond the single apply() call and adds no runtime dependencies of its own, making it a common low-ceremony fix wherever nested async execution is unavoidable.

What You Get

  • A single apply(loop=None) function that patches the current (or a given) event loop to be reentrant
  • Compatibility patches for asyncio’s Task/Future classes across supported Python versions
  • An event loop policy patch so any newly created loop is automatically patched
  • Optional Tornado Future compatibility patching when Tornado is already imported
  • Zero runtime dependencies and a single-file implementation that’s easy to audit

Common Use Cases

  • Running loop.run_until_complete() from inside a Jupyter or IPython notebook cell that already has a running event loop
  • Calling blocking async code from within a GUI application’s own event loop (e.g. Qt, Tkinter integrations)
  • Allowing libraries or test harnesses that call asyncio.run() internally to work when invoked from an already-running async context
  • Unblocking synchronous wrapper functions around async APIs inside frameworks that manage their own asyncio loop

Under The Hood

Architecture - The entire library lives in one 220-line module, nest_asyncio.py. apply(loop=None) orchestrates three monkey-patch passes: _patch_asyncio() rebinds asyncio.Task/asyncio.Future to their pure-Python implementations and replaces asyncio.run; _patch_policy() wraps the event loop policy’s get_event_loop so any freshly created loop is patched automatically; _patch_loop(loop) replaces the loop class’s run_forever, run_until_complete, and _run_once methods with reentrant-safe versions that temporarily pop and restore the ‘current task’ bookkeeping around each handle invocation so nested run_until_complete calls don’t trip asyncio’s own reentrancy guards. A manage_run context manager tracks nested-run depth and IOCP proactor state on Windows. Idempotency is guarded by _nest_patched flags on both the module and the loop instance. Tech Stack - Pure standard-library Python with no runtime dependencies; the only external interaction is an optional patch of Tornado’s Future class if Tornado is already imported. Packaging uses a plain setup.py/setup.cfg plus a pyproject.toml for build-system metadata; there is no compiled extension or platform-specific code besides a Windows IOCP proactor-loop branch. Code Quality - The test suite (tests/nest_test.py) uses unittest and covers the core scenarios directly: two and three levels of nested run_until_complete, ensure_future combined with nested runs, asyncio.wait, timeout propagation, running two nested loops within one outer loop, and context-variable propagation across nested runs. There are no type annotations and no linting/formatting config beyond defaults, but given the module’s small, single-purpose surface area this is proportionate; naming is clear and each patched method carries a docstring explaining its divergence from the stdlib original. API Design - The public surface is a single function, apply(loop=None), which is about as low-friction as an API can get — no configuration object, no class to instantiate, and it can be called defensively at import time without side effects on an already-patched loop. This makes onboarding trivial: one line unblocks the exact failure users hit, at the cost of relying on monkey-patching private asyncio internals, which is an inherent, load-bearing tradeoff for what the library needs to accomplish.

Used by 8 apps in this directory

Python
100%
Apache 2.0

Agno

Devops · AI Development · Automation

41,774

Build, run, and manage agent platforms with a full production stack — SDK, runtime, and control plane included.

View details
93
Repo Health
87
Technical
67
Dependency
Built with
Python100%
Updated today
Python
59%
Apache 2.0

argilla

AI Development · Data Engineering

5,081

Collaborate on high-quality AI training data with a self-hosted annotation platform built for LLMs, NLP, and multimodal models.

View details
65
Repo Health
81
Technical
63
Dependency
Built with
Python59%
Jupyter Notebook21%
Updated 2 days ago
Python
88%
Apache 2.0

ART

AI Development

10,603

Give your LLM agents on-the-job training—ART lets you apply GRPO reinforcement learning to any multi-step agentic workflow with minimal code changes.

View details
85
Repo Health
82
Technical
72
Dependency
Built with
Python88%
Cuda10%
Updated today
TypeScript
72%
AGPL 3.0

Firecrawl

AI Development · Developer Tools

169,311

Turn any website into clean, LLM-ready data with a single API call — no proxy headaches, no scraping complexity.

View details
89
Repo Health
83
Technical
66
Dependency
Built with
TypeScript72%
Python14%
Updated today
Python
63%
Apache 2.0

GPT Researcher

Productivity · AI Assistants

29,039

The pioneering open-source autonomous AI agent that conducts deep, multi-source research and produces citation-backed reports exceeding 2,000 words — faster and more reliably than any human researcher.

View details
88
Repo Health
91
Technical
64
Dependency
Built with
Python63%
TypeScript23%
Updated 1 months ago
Python
68%
MIT

Langflow

AI Agents · AI Development

153,450

Build, test, and deploy AI agents and RAG workflows visually with native API and MCP server export.

View details
90
Repo Health
85
Technical
66
Dependency
Built with
Python68%
TypeScript22%
Updated today
Python
85%
Apache 2.0

PrivateGPT

AI Development

57,445

The open-source API layer that turns local LLMs into production private AI applications with full Claude API compatibility

View details
88
Repo Health
83
Technical
71
Dependency
Built with
Python85%
HTML11%
Updated today
JavaScript
50%
AGPL 3.0

QRev

CRM · AI Agents

362

Open source AI-first sales platform that replaces Salesforce with autonomous agents handling prospecting, outreach, and lead management at scale.

View details
39
Repo Health
68
Technical
65
Dependency
Built with
JavaScript50%
Python28%
TypeScript14%
Updated 6 months ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search