Python package that makes it easy to run fast asyncio code
Are you trying to run async code in Python and getting the error
asyncio.run() cannot be called from a running event loop
?
This package makes it easy to run asyncio-based async code in tricky execution environments:
- IPython-based notebooks (JupyterLab, AWS Sagemaker, Databricks, etc)
- trio applications
- curio applications
Other Features:
- Use uvloop for faster i/o - but without permanently changing the asyncio event loop policy
- Run any code which uses asyncio I/O.
- Type hints to support type-aware IDEs
Non-Features:
- asyncio-anywhere does not monkey-patch the asyncio module. This avoids unexpected side-effects and potential future bugs.
pip install asyncio-anywhere
import asyncio
from asyncio_anywhere import asyncio_run
async def myfunc():
await asyncio.sleep(0.01)
return "OK"
result = asyncio_run(myfunc())
print(result)
Use asyncio_run()
anywhere where you would normally have run asyncio.run(). Note that it also accepts a boolean debug
parameter as the second parameter.