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.4.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.4.0-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aiointerpreters-0.4.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.4.0.tar.gz
Algorithm Hash digest
SHA256 26493412aa18252c84b7867a8f3e139a245030b9b11c7eba2fe733c7903454e0
MD5 d5c49ddf84623d79a617d54b8bb2ad18
BLAKE2b-256 3b76c59a200886cdf5f5b8b5cb0f4944541fd3f706057680e9425dc0d2e01fb0

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for aiointerpreters-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e663086c25781e90272a03b50eb8864f9ed54587f1f7b08cceb4e168ed3daf33
MD5 f527b6f52f446bfbb3003ef6ea1ad1d0
BLAKE2b-256 a8b9507b03ae9235bdb25f476b2c8f6aa1aa8104672ab85dce84267a08e63271

See more details on using hashes here.

Provenance

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