Skip to main content

An asynchronous Python Jupyter kernel

Project description

Build Status Code style: black

akernel

An asynchronous Python Jupyter kernel.

Motivation

ipykernel offers the ability to run asynchronous code from the REPL. This means you can await at the top-level, outside of a function. Unfortunately, this will still block the kernel.

akernel changes this behavior by launching each cell in a task.

Features

akernel allows for asynchronous code execution. What this means is that when used in a Jupyter notebook, you can run cells concurrently if the code is cooperative. For instance, you can run a cell with the following code:

# cell 1
for i in range(10):
    print("cell 1:", i)
    await asyncio.sleep(1)

Since this cell is async (it has an await), it will not block the execution of other cells. So you can run another cell concurrently, provided that this cell is also cooperative:

# cell 2
for j in range(10):
    print("cell 2:", j)
    await asyncio.sleep(1)

If cell 2 was blocking, cell 1 would pause until cell 2 was finished. You can see that by changing await asyncio.sleep(1) into time.sleep(1) in cell 2.

You can make a cell wait for the previous one to be finished with:

# cell 2
await __task__()  # wait for cell 1 to be finished
print("cell 1 has run")

Limitations

It is still a work in progress, and a bit hacky. In particular:

  • Error tracebacks are a bit messy.
  • stdout/stderr redirection to the cell output is only supported through the print function.
  • No rich representation for now, only the standard __repr__ is supported. This means no matplotlib figure yet :-( But ipywidgets should work!
  • If the cell code has multiline strings, they must be wrapped with the textwrap.dedent function.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

akernel-0.0.8.tar.gz (12.5 kB view hashes)

Uploaded Source

Built Distribution

akernel-0.0.8-py3-none-any.whl (14.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page