Skip to main content

Run CPU bound code with subinterpreters using asyncio.

Project description

aiointerpreters

Run CPU bound code in subinterpreters using asyncio.

Installation

Pick the tool of your choosing

uv add aiointerpreters
uv pip install aiointerpreters
pip install aiointerpreters
poetry add aiointerpreters
pdm add aiointerpreters
pipenv install aiointerpreters

Basic Usage

Suppose you want ot run some CPU bound function in parallel.

Create a Runner and call the function in parallel using asyncio's gather or TaskGroup:

with Runner(workers=5).start() as runner:
    await asyncio.gather(runner.run(cpu_bound_function(5), cpu_bound_function(5)))

Restrictions

The CPU bound function must be importable and a top level function. All its arguments and return value must be Shareable:

type Shareable = (
    str | bytes | int | float | bool | None | tuple[Shareable, ...] | Queue | memoryview
)

Motivation

The concurrent.interpreters api will be added in Python 3.14.

There are several high level mechanisms to access subinterpreters, such as Interpreter.call_in_thread and InterpreterPoolExecutor. But they both come with drawbacks:

  • call_in_thread: only works for top level functions with no arguments.
  • InterpreterPoolExecutor: uses pickles to pass data between interpreters similar to ProcessPoolExecutor.

There are no simple options to take advantage of the fast shared memeory.

Architecture

We must run the interpreters in threads to achieve parallelism. In order to notify futures in the background we must also have a coordniator thread to handle the results.

sequenceDiagram
  participant M as Main
  participant C as Coordinator
  participant W1 as Worker 1
  participant W2 as Worker 2
  M ->> W1: task queue
  M ->> W2: task queue
  W1 -->> C: result queue
  C -->> M: notify
  W2 -->> C: result queue
  C -->> M: set_result

Examples

See examples.

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

aiointerpreters-0.2.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiointerpreters-0.2.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file aiointerpreters-0.2.0.tar.gz.

File metadata

  • Download URL: aiointerpreters-0.2.0.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for aiointerpreters-0.2.0.tar.gz
Algorithm Hash digest
SHA256 100eab1af03c7476b40a938fbd08084c382c3f512e6c89be95b0dd886571714c
MD5 3c000bb39a27ee6245eaf801b508f62e
BLAKE2b-256 749bc964e8add7ec245a7e1f29783305534feeac998071e5bfbb9ce3b8702eb3

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiointerpreters-0.2.0.tar.gz:

Publisher: python-publish.yml on Jamie-Chang/aiointerpreters

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file aiointerpreters-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for aiointerpreters-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 244f1a9f29ed7f799da66b5198ab64f9c7a352683399a5dda70c48501d058df7
MD5 9def1c203922855cc30e31de1e810280
BLAKE2b-256 6347dba17844d4d082697842cacdab29773f4cbe4ff68df1564008be9cd7524d

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiointerpreters-0.2.0-py3-none-any.whl:

Publisher: python-publish.yml on Jamie-Chang/aiointerpreters

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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