Is it possible to have always running event loop in ipython similar to how it's done in jupyter notebook?

Viewed 316

In Jupyter there is always running event loop which allows you to interactively execute code while having some coroutines running in background (e.g. launched with asyncio.create_task ). Such behavior makes testing concurrent code much easier. Is it possible to somehow achieve same behavior in ipython?

I use python 3.8 and ipython 7.12.

1 Answers

You can achieve this with aioconsole. Just install it with pip :

pip3 install aioconsole

You can then run the apython command to run the interactive asynchronous python shell in which you can run asynchronous code. Example of usage :

$ apython
Python 3.7.3 (default, Jan 22 2021, 20:04:44) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
---
This console is running in an asyncio event loop.
It allows you to wait for coroutines using the '{0}' syntax.
Try: await asyncio.sleep(1, result=3)
---
>>> await asyncio.sleep(1, result=3)
3
>>> 
Related