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

Runner

See runner docs, initially the library only exposes a Runner class with a custom interface to schedule asyncio tasks.

Since the official release of 3.14 and the stablisation of a lot of the interpreter features, this offers no advantage to the standard library InterpreterPoolExecutor and therefore is not something that will be updated going forward.

Executor

This library implements a new InterpreterThreadPoolExecutor. This is a drop in replacement for the standard ThreadPoolExecutor with options to dynamically switch to using an Interpreter.

from aiointerpreters.executors import InterpreterThreadPoolExecutor, interpreter


with InterpreterThreadPoolExecutor() as executor:
    executor.map(interpreter(cpu_bound), (argument for _ in range(runs)))

Any function tagged with interpreter decorator will be dispatched to an isolated interpreter in a separate thread. Otherwise this follows the original behaviour of ThreadPoolExecutor.

Additionally, InterpreterThreadPoolExecutor can be set as the default executor for asyncio and used with asyncio.to_thread:

# Shortcut 
InterpreterThreadPoolExecutor().set_as_event_loop_default()

# or 
asyncio.get_running_loop().set_default_executor(InterpreterThreadPoolExecutor())

async with asyncio.TaskGroup() as tg:
    tg.create_task(asyncio.to_thread(interpreter(cpu_bound), argument))
    tg.create_task(asyncio.to_thread(io_bound, argument))

Restrictions

The CPU bound function must be picklable. All its arguments and return value must be pickables or Shareable:

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

Motivation

concurrent.future.InterpreterPoolExecutor is currently a great choice if you know you always need an interpreter to run your code. However it's rarely something you want to do by default since you have to always consider the restrictions mentioned above.

You can choose to run a ThreadPoolExecutor alongside an InterpreterPoolExecutor but since they both maintain a pool of threads under the hood, it'll actually save some resources to share the pool of threads. And having one good default option just makes things easier.

Using asyncio.to_thread

Note: since ContextVars are not pickleable InterpreterThreadPoolExecutor works around it by discarding the context, this is not slightly hacky and may change in the future.

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.6.0.tar.gz (9.4 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.6.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for aiointerpreters-0.6.0.tar.gz
Algorithm Hash digest
SHA256 08bb9d99f29911034c2903e9f63f6ef0f691014e96da71644693c21a8257a54a
MD5 c013164feab572b2a4edcf71d6a42a3c
BLAKE2b-256 eb8cae972cfec27ae47764dd17684c852276effc9e55491c855ea208e163fe8a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for aiointerpreters-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c6615c033375d09420acdd0f51039a4d143e3515cb7cf108bbf7d71a2c41e6e0
MD5 705988324b755df649348789b07a1b45
BLAKE2b-256 3d4028e9411a0d03482d972693e33c0a492e128de60ed0707c610ce44b9c24dc

See more details on using hashes here.

Provenance

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