Giter VIP home page Giter VIP logo

nest_asyncio's Introduction

Build PyPi Number of downloads

Introduction

By design asyncio does not allow its event loop to be nested. This presents a practical problem: in an environment where the event loop is already running, it's impossible to run tasks and wait for the result. Attempting to do so will lead to a "RuntimeError: This event loop is already running".

This issue pops up in various environments, including web servers, GUI applications and in Jupyter notebooks.

This module patches asyncio to enable nested usage of asyncio.run and loop.run_until_complete.

Installation

pip3 install nest_asyncio

Python 3.5 or higher is required.

Usage

from nest_asyncio import NestedAsyncIO
with NestedAsyncIO():
    ...

Wrap any code requiring nested runs with a NestedAsyncIO context manager or manually call apply and revert on demand. Optionally, a specific loop may be supplied as an as argument to apply or the constructor if you do not wish to patch the the current event loop. An event loop may be patched regardless of its state, running or stopped. Note that this packages is limited to asyncio event loops: general loops from other projects, such as uvloop or quamash, cannot be patched.

nest_asyncio's People

Contributors

erdewit avatar charliejiangxxx avatar diego-plan9 avatar stnatter avatar uriyyo avatar 5j9 avatar dsblank avatar gsmecher avatar crusaderky avatar hugovk avatar unkcpz avatar kolanich avatar bollwyvl avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.