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:

  • 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.3.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.3.0-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aiointerpreters-0.3.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.3.0.tar.gz
Algorithm Hash digest
SHA256 03663262a14b908af302d6c98841d5d40d35f6931da2d1427a5a69512eafbeba
MD5 b5e79e973f361748e52eaecfceb3f569
BLAKE2b-256 28682e13e83ee64bfc38a0c06147c5e90bfe651970073a00adc78b11e0e3cdfa

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for aiointerpreters-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e0ae79f728f41686c23d0fbcfbca9eca0aca20f6429c8fcc5813a6d1127e0adc
MD5 69cd0a21d07e0b0638500317ee38aac7
BLAKE2b-256 a62572ddad1918c4c2e7e2acda7309b4a5be74422d978ea9624b7e86979a3cbf

See more details on using hashes here.

Provenance

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