A concurrent python kernel for Jupyter supporting AnyIO, AsyncIO and Trio.
Project description
async-kernel
async-kernel is a Python 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, VS Code, etc).
- An experimental callback style interface (Jupyterlite).
Highlights
- IPython shell
- top-level await ('asyncio' or 'trio' backend) in cells
- async magic function support in cells
- anyio compatible asynchronous backend (
asyncio(default) ortrio) - aiologic thread-safe synchronisation primitives
- Backend agnostic multi-thread / multi-event loop management
- Per-subshell user_ns
- GUI event loops 1
- Experimental support for
Jupyterlite (try it online here 👈)
%pip installmagic (using micropip)
- Debugger client
Avoid deadlocks
The standard (synchronous) kernel implementation processes messages sequentially irrespective of the message type. The problem being that long running execute requests 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 the blocking execute request4.
async-kernel handles messages according to the channel and message type. 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.
Example
Make a blocking call in a Jupyter lab notebook or console.
# Make the shell's thread busy
import time
time.sleep(1e6)
While the above is blocking (the kernel is busy).
dir() # try code completion (tab) or view the docstring (shift tab)
Interrupt the kernel.
It also works for awaitables.
import ipywidgets as ipw
from aiologic import Event
b = ipw.Button(description="Click me")
event = Event()
b.on_click(lambda _: event.set())
display(b)
await event
Installation
pip install async-kernel
Kernelspecs
A kernelspec with the name 'async' is added when async-kernel is installed.
Kernel specs can be installed/uninstalled via the command line.
async-kernel install
# To install for a user
async-kernel install --user
For further detail about kernel spec customisation see command line and kernel configuration and custom kernel.ipynb.
Faster data serialization
orjson (a fast JSON library) is supported and will be used by default if it has been installed.
Free-threading support
async-kernel's Caller's are thread-local and it's methods are internally synchronised5.
Origin
async-kernel started as a fork of IPyKernel. Thank you to the original contributors of IPyKernel that made async-kernel possible.
-
A gui (host) enabled kernel interface starts a gui's mainloop (host) which starts the backend as a guest, then finally the Kernel is started. ↩
-
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
-
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. ↩
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file async_kernel-0.20.2.tar.gz.
File metadata
- Download URL: async_kernel-0.20.2.tar.gz
- Upload date:
- Size: 329.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
79a70b86492dfdf543283309c66d072a5d353bab37f696a72d82913271af32ea
|
|
| MD5 |
cc73b5d03db2a8c548a2c497baf8edf4
|
|
| BLAKE2b-256 |
5ea5b7e34eba88f3e9c2e778f939f40b242b082fab24e329eac7b695b41f237d
|
Provenance
The following attestation bundles were made for async_kernel-0.20.2.tar.gz:
Publisher:
publish-to-pypi.yml on fleming79/async-kernel
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
async_kernel-0.20.2.tar.gz -
Subject digest:
79a70b86492dfdf543283309c66d072a5d353bab37f696a72d82913271af32ea - Sigstore transparency entry: 2017843866
- Sigstore integration time:
-
Permalink:
fleming79/async-kernel@b079011e53095b87804018b1e5ad831bd5b6abb9 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/fleming79
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@b079011e53095b87804018b1e5ad831bd5b6abb9 -
Trigger Event:
workflow_run
-
Statement type:
File details
Details for the file async_kernel-0.20.2-py3-none-any.whl.
File metadata
- Download URL: async_kernel-0.20.2-py3-none-any.whl
- Upload date:
- Size: 119.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a2877ce62499fa16bad1bf56f39389ec4d1182cbc388d83c9e85cc724192bdf
|
|
| MD5 |
a3c17bad266cc4036d1dc74480ddecfe
|
|
| BLAKE2b-256 |
8e6b6c1a4520f6d0bf3dd18835798c2d7077df1b6dfd7459bb25b2c6c738016b
|
Provenance
The following attestation bundles were made for async_kernel-0.20.2-py3-none-any.whl:
Publisher:
publish-to-pypi.yml on fleming79/async-kernel
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
async_kernel-0.20.2-py3-none-any.whl -
Subject digest:
6a2877ce62499fa16bad1bf56f39389ec4d1182cbc388d83c9e85cc724192bdf - Sigstore transparency entry: 2017843936
- Sigstore integration time:
-
Permalink:
fleming79/async-kernel@b079011e53095b87804018b1e5ad831bd5b6abb9 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/fleming79
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-to-pypi.yml@b079011e53095b87804018b1e5ad831bd5b6abb9 -
Trigger Event:
workflow_run
-
Statement type: