nest-asyncio Library

1.6.0 · active · verified Sat Mar 28

nest-asyncio is a Python library that patches the `asyncio` module to allow nested event loops. This addresses the `RuntimeError: This event loop is already running` issue encountered in environments like web servers, GUI applications, and Jupyter notebooks where an event loop is already active. It is currently at version 1.6.0 and maintains a stable release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to apply the `nest-asyncio` patch and then run an `asyncio` coroutine using `asyncio.run()`, even if an event loop is already running.

import asyncio
import nest_asyncio

# Apply the patch to allow nested event loops
nest_asyncio.apply()

async def my_async_function():
    await asyncio.sleep(0.1)
    return "Hello, Nested World!"

# This works even in environments with existing event loops
result = asyncio.run(my_async_function())
print(result)

view raw JSON →