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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
08bb9d99f29911034c2903e9f63f6ef0f691014e96da71644693c21a8257a54a
|
|
| MD5 |
c013164feab572b2a4edcf71d6a42a3c
|
|
| BLAKE2b-256 |
eb8cae972cfec27ae47764dd17684c852276effc9e55491c855ea208e163fe8a
|
Provenance
The following attestation bundles were made for aiointerpreters-0.6.0.tar.gz:
Publisher:
python-publish.yml on Jamie-Chang/aiointerpreters
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aiointerpreters-0.6.0.tar.gz -
Subject digest:
08bb9d99f29911034c2903e9f63f6ef0f691014e96da71644693c21a8257a54a - Sigstore transparency entry: 786501745
- Sigstore integration time:
-
Permalink:
Jamie-Chang/aiointerpreters@003a6de3ae460c7b99669bfe979707aaeed06ce7 -
Branch / Tag:
refs/tags/v0.6.0 - Owner: https://github.com/Jamie-Chang
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@003a6de3ae460c7b99669bfe979707aaeed06ce7 -
Trigger Event:
release
-
Statement type:
File details
Details for the file aiointerpreters-0.6.0-py3-none-any.whl.
File metadata
- Download URL: aiointerpreters-0.6.0-py3-none-any.whl
- Upload date:
- Size: 10.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c6615c033375d09420acdd0f51039a4d143e3515cb7cf108bbf7d71a2c41e6e0
|
|
| MD5 |
705988324b755df649348789b07a1b45
|
|
| BLAKE2b-256 |
3d4028e9411a0d03482d972693e33c0a492e128de60ed0707c610ce44b9c24dc
|
Provenance
The following attestation bundles were made for aiointerpreters-0.6.0-py3-none-any.whl:
Publisher:
python-publish.yml on Jamie-Chang/aiointerpreters
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aiointerpreters-0.6.0-py3-none-any.whl -
Subject digest:
c6615c033375d09420acdd0f51039a4d143e3515cb7cf108bbf7d71a2c41e6e0 - Sigstore transparency entry: 786501747
- Sigstore integration time:
-
Permalink:
Jamie-Chang/aiointerpreters@003a6de3ae460c7b99669bfe979707aaeed06ce7 -
Branch / Tag:
refs/tags/v0.6.0 - Owner: https://github.com/Jamie-Chang
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@003a6de3ae460c7b99669bfe979707aaeed06ce7 -
Trigger Event:
release
-
Statement type: