Skip to main content

Run multiple functions in parallel and collect results with the simplest possible API

Project description

philiprehberger-run-parallel

Tests PyPI version License

Run multiple functions in parallel and collect results with the simplest possible API.

Install

pip install philiprehberger-run-parallel

Usage

Run functions in parallel

from philiprehberger_run_parallel import parallel

results = parallel(
    lambda: 1 + 1,
    lambda: 2 + 2,
    lambda: 3 + 3,
)
# [2, 4, 6]

Pass arguments with tuples

from philiprehberger_run_parallel import parallel
import time

results = parallel(
    (time.sleep, 0.1),
    (pow, 2, 10),
)
# [None, 1024]

Map a function over items

from philiprehberger_run_parallel import parallel_map

results = parallel_map(str.upper, ["hello", "world"])
# ["HELLO", "WORLD"]

# Control the number of workers
results = parallel_map(fetch_url, urls, workers=8, timeout=30)

Async parallel

import asyncio
from philiprehberger_run_parallel import aparallel

async def main():
    results = await aparallel(
        fetch("https://example.com/a"),
        fetch("https://example.com/b"),
    )
    print(results)

asyncio.run(main())

Error handling

from philiprehberger_run_parallel import parallel, ParallelError

try:
    results = parallel(
        lambda: 1,
        lambda: 1 / 0,
    )
except ParallelError as e:
    print(e.errors)   # [None, ZeroDivisionError(...)]
    print(e.results)  # [1, None]

API Reference

Function / Class Description
parallel(*tasks, timeout=None) -> list Run callables or (fn, *args) tuples via ThreadPoolExecutor, return results in order.
parallel_map(fn, items, *, workers=0, timeout=None) -> list Apply a function to each item in parallel, return results in order.
aparallel(*coros) -> list Run async coroutines concurrently via asyncio.gather, return results in order.
ParallelError Raised when any task fails. Has .errors (list of exceptions/None) and .results (list of values/None).

License

MIT

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

philiprehberger_run_parallel-0.1.2.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_run_parallel-0.1.2-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_run_parallel-0.1.2.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_run_parallel-0.1.2.tar.gz
Algorithm Hash digest
SHA256 32ce78bfb07998d8a9d43fec9c9779cc94ae930a7b2dc08394dffb96967aaa27
MD5 8afa5befe09412a1b9fa674490e22145
BLAKE2b-256 2d943dd3daae33a56ee8572f385444273e42e3326b6a05d2d210f8fea8252ae6

See more details on using hashes here.

File details

Details for the file philiprehberger_run_parallel-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_run_parallel-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 db4e4f63a7d90af9d4b7ce5046f2d35f5c542412ae85bc83923f4d7f9ff79b00
MD5 5f1da2208f8534e980bbe61fd299c57d
BLAKE2b-256 205af33e7b9e9fe7bed2539d8ef62d935d908b20a9205575b0d96f4898d2466d

See more details on using hashes here.

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