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), runner.run(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:

  • InterpreterPoolExecutor: uses pickles to pass data between interpreters similar to ProcessPoolExecutor.
  • call_in_thread: will use fast shared memory to pass arguments but has no mechanism to retrieve the results. It's also a lot harder to reuse interpreters.

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.5.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.5.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for aiointerpreters-0.5.0.tar.gz
Algorithm Hash digest
SHA256 af3203f8afab73f292bb9ab377a97d5ef64ef7e0b5dd1df9c2bca3118e561f71
MD5 79058c5a07ade9f607a0a8b974d59611
BLAKE2b-256 1229fe291a44fcde7ce9c055e25facbfea1d3abb02f32b0ea40142aae8daa6c0

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiointerpreters-0.5.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.5.0-py3-none-any.whl.

File metadata

File hashes

Hashes for aiointerpreters-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 adfc1b23807afda82cfaeb3195c3393028bcfcb1b0fe2b1f7b664e2569dbafaf
MD5 ac2235a58ed010a3e3b1b8dae486492c
BLAKE2b-256 044feabf6c0a41d7f63c1faf7290712a54938ab37f968bd29d43128824d5e81f

See more details on using hashes here.

Provenance

The following attestation bundles were made for aiointerpreters-0.5.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