nest_asyncio Documentation

repository·master·Indexed 20 days ago

https://github.com/erdewit/nest_asyncio

A utility that patches the asyncio module to allow nested event loops, preventing 'RuntimeError: This event loop is already running' in environments where a loop is already active, such as Jupyter notebooks, web servers, or GUI applications. It enables nested use of asyncio.run and loop.run_until_complete via nest_asyncio.apply().

Tokens
231
Snippets
2
Records
2
Agent score
23%

What's inside nest_asyncio

  1. How to patch asyncio to allow nested event loops

    master

    By default, asyncio does not allow its event loop to be nested, which causes a RuntimeError: This event loop is already running when attempting to run tasks in environments like Jupyter notebooks, web servers, or GUI applications.

    nest_asyncio patches asyncio to allow nested use of asyncio.run and loop.run_until_complete.

    To use it, call nest_asyncio.apply(). You can optionally pass a specific event loop as an argument to apply() to patch it; otherwise, the current event loop is used. Note that only asyncio event loops can be patched; loops from other projects like uvloop or quamash generally cannot be patched.

    import nest_asyncio
    nest_asyncio.apply()