Skip to main content

CPU parallelism for Trio

Project description

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

MIT -or- Apache License 2.0

Documentation

Documentation

Chat

Chatroom

Forum

Forum

Issues

Issues

Repository

Repository

Tests

Tests

Coverage

Test coverage

Style

Code style

Distribution

Latest Pypi version
Supported Python versions
Supported Python interpreters

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, cancellable=True
            )
        )
        await trio.sleep(2)
        # Only explicitly cancellable jobs are killed on cancel
        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.

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

trio-parallel-1.0.0.tar.gz (49.4 kB view details)

Uploaded Source

Built Distribution

trio_parallel-1.0.0-py3-none-any.whl (34.5 kB view details)

Uploaded Python 3

File details

Details for the file trio-parallel-1.0.0.tar.gz.

File metadata

  • Download URL: trio-parallel-1.0.0.tar.gz
  • Upload date:
  • Size: 49.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for trio-parallel-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7be741cf1ab925c97a5f6f334aee469c3e9c98124a0229b4ed2bfe8563940c13
MD5 40599e2a6da829e03da35a4619a0f866
BLAKE2b-256 d8932538e2efa8333b4c02b762d06cdc7bfaf610118e4edf45a4c1bbf0578aea

See more details on using hashes here.

File details

Details for the file trio_parallel-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: trio_parallel-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 34.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for trio_parallel-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e554634fe4933e36a09f69827801c969e54c610dca70cdf5d8661c0d52565b6a
MD5 68da3fa13bc5ddbc16ec401568696698
BLAKE2b-256 56b6031f1c48f1587aae8fbe70db25a694f6b035a914d29563be34a1605dbdc7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page