trio-parallel: CPU parallelism for Trio
Do you have CPU-bound work that just keeps slowing down your Trio event loop no matter what you try? Do you need to get all those cores humming at once? This is the library for you!
The aim of trio-parallel is to use the lightest-weight, lowest-overhead, lowest-latency method to achieve CPU parallelism of arbitrary Python code with a dead-simple API.
Resources
License |
|
Documentation |
|
Chat |
|
Forum |
|
Issues |
|
Repository |
|
Tests |
|
Coverage |
|
Style |
|
Distribution |
Example
import functools
import multiprocessing
import trio
import trio_parallel
def loop(n):
# Arbitrary CPU-bound work
for _ in range(n):
pass
print("Loops completed:", n)
async def amain():
t0 = trio.current_time()
async with trio.open_nursery() as nursery:
# Do CPU-bound work in parallel
for i in [6, 7, 8] * 4:
nursery.start_soon(trio_parallel.run_sync, loop, 10 ** i)
# Event loop remains responsive
t1 = trio.current_time()
await trio.sleep(0)
print("Scheduling latency:", trio.current_time() - t1)
# This job could take far too long, make it cancellable!
nursery.start_soon(
functools.partial(
trio_parallel.run_sync, loop, 10 ** 20, kill_on_cancel=True
)
)
await trio.sleep(2)
# Only explicit kill_on_cancel jobs are terminated
nursery.cancel_scope.cancel()
print("Total runtime:", trio.current_time() - t0)
if __name__ == "__main__":
multiprocessing.freeze_support()
trio.run(amain)
Additional examples and the full API are available in the documentation.
Features
Bypasses the GIL for CPU-bound work
Minimal API complexity
looks and feels like Trio threads
Minimal internal complexity
No reliance on multiprocessing.Pool, ProcessPoolExecutor, or any background threads
Cross-platform
print just works
Seamless interoperation with
Automatic LIFO caching of subprocesses
Cancel seriously misbehaving code via SIGKILL/TerminateProcess
Convert segfaults and other scary things to catchable errors
FAQ
How does trio-parallel run Python code in parallel?
Currently, this project is based on multiprocessing subprocesses and has all the usual multiprocessing caveats (freeze_support, pickleable objects only, executing the __main__ module). The case for basing these workers on multiprocessing is that it keeps a lot of complexity outside of the project while offering a set of quirks that users are likely already familiar with.
The pickling limitations can be partially alleviated by installing cloudpickle.
Can I have my workers talk to each other?
This is currently possible through the use of multiprocessing.Manager, but we don’t and will not officially support it.
This package focuses on providing a flat hierarchy of worker subprocesses to run synchronous, CPU-bound functions. If you are looking to create a nested hierarchy of processes communicating asynchronously with each other, while preserving the power, safety, and convenience of structured concurrency, look into tractor. Or, if you are looking for a more customized solution, try using trio.run_process to spawn additional Trio runs and have them talk to each other over sockets.
Can I let my workers outlive the main Trio process?
No. Trio’s structured concurrency strictly bounds job runs to within a given trio.run call, while cached idle workers are shutdown and killed if necessary by our atexit handler, so this use case is not supported.
How should I map a function over a collection of arguments?
This is fully possible but we leave the implementation of that up to you. Think of us as a loky for your joblib, but natively async and Trionic. We take care of the worker handling so that you can focus on the best concurrency for your application. That said, some example parallelism patterns can be found in the documentation.
Also, look into aiometer?
Contributing
If you notice any bugs, need any help, or want to contribute any code, GitHub issues and pull requests are very welcome! Please read the code of conduct.
Metadata
Release files for trio-parallel 1.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| trio_parallel-1.3.0.tar.gz | 54.0 kB | With provenance | Details for trio_parallel-1.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| trio_parallel-1.3.0-py3-none-any.whl | Python 3 | none | any | Details for trio_parallel-1.3.0-py3-none-any.whl |
Total release size: 91.2 kB
Release files / trio_parallel-1.3.0.tar.gz
| Download URL | trio_parallel-1.3.0.tar.gz |
|---|---|
| Size | 54.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
db1242a1923883ce15511465c98f2f437d6bc69596b1afe2efd9782ef1387107
|
|
BLAKE2b-256 checksum How to use checksums |
4b6a47bb4fb4b67e3f25c8fc30ae877876fed0f378cb7d9a12115033afe88d20
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 25, 2024.
Transparency logRelease files / trio_parallel-1.3.0-py3-none-any.whl
| Download URL | trio_parallel-1.3.0-py3-none-any.whl |
|---|---|
| Size | 37.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5e6005875171d55b5eca143e2150e436457fe207814ba1f1b0b19aae0b83035b
|
|
BLAKE2b-256 checksum How to use checksums |
8dc0dd3e9ba6ab33c6c9f6dcd897b37fa6cadf871845440732c967f921736779
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.0.1 CPython/3.12.8
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 25, 2024.
Transparency logSecurity for trio-parallel 1.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for trio_parallel-1.3.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 25, 2024.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.