Async kernel is an IPython kernel for Jupyter that provides concurrent message handling via an asynchronous backend (asyncio or trio).
The kernel provides two external interfaces:
- Direct ZMQ socket messaging via a configuration file and kernel spec - (Jupyter, VScode, etc).
- An experimental callback style interface (Jupyterlite).
- Debugger client
- anyio compatible asynchronous backend (
asyncio(default) ortrio) - aiologic thread-safe synchronisation primitives
- Backend agnostic multi-thread / multi-event loop management
- IPython shell
- Per-subshell user_ns
- GUI event loops 12
- Experimental support for Jupyterlite (try it online here 👈)
The standard (synchronous) kernel implementation processes messages sequentially irrespective of the message type. The problem being that long running execute requests will make the kernel non-responsive. Another problem exists when an asynchronous execute request awaits a result that is delivered via a kernel message - this will cause a deadlock because the message will be stuck in the queue behind theblocking execute request5.
Async kernel handles messages according to the channel, message type and subshell id. So widget com message will get processed in a separate queue to an execute request. Further detail is given in the concurrency notebook, a Jupyterlite version is available here.
pip install async-kernelA kernel spec with the name 'async' is added when async kernel is installed.
Kernel specs can be added/removed via the command line.
The kernel is configured via the interface with the options:
namedisplay_name- Parameters on the kernel including:
interface.backendinterface.backend_optionsinterface.loopinterface.loop_optionsshell.timeout
The backend set on the interface is the asynchronous library the kernel uses for message handling. It is also the asynchronous library directly available when executing code in cells or via a console4.
pip install trio
async-kernel -a async --interface.backend=trioThe kernel can be started with a gui event loop as the host and the backend running as a guest.
# tk
async-kernel -a async-tk --interface.loop=tk
# qt
pip install PySide6-Essentials
async-kernel -a async-qt --interface.loop=qtpip install trio
# tk
async-kernel -a async-tk --interface.loop=tk --interface.backend=trio
# qt
pip install PySide6-Essentials
async-kernel -a async-qt --interface.loop=qt --interface.backend=trioFor further detail about kernel spec customisation see command line and kernel configuration.
Async kernel started as a fork of IPyKernel. Thank you to the original contributors of IPyKernel that made Async kernel possible.
Footnotes
-
A gui (host) enabled kernel runs a gui event loop with the asynchronous backend running as guest. The host must be set before the kernel is started. This was a deliberate design choice to to ensure good performance and reliability. ↩
-
It is also possible to use a caller to run a gui event loop in a separate thread (with a backend running as a guest) if the gui allows it (qt will only run in the main thread). Also note that pyplot will only permit one interactive gui library in a process. ↩
-
The asyncio implementation of
start_guest_runwas written by the author of aiologic and provided as a gist. ↩ ↩2 -
trio's start_guest_run. ↩ ↩2 ↩3
-
Ipykernel solves this issue specifically for widgets by using the concept of 'widget coms over subshells'. Widget messages arrive in a different thread which on occasion can cause unexpected behaviour, especially when using asynchronous libraries. ↩