Run multiple functions in parallel and collect results with the simplest possible API
Project description
philiprehberger-run-parallel
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). |
Development
pip install -e .
python -m pytest tests/ -v
License
MIT
Project details
Release history Release notifications | RSS feed
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 philiprehberger_run_parallel-0.1.4.tar.gz.
File metadata
- Download URL: philiprehberger_run_parallel-0.1.4.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ec7a691dafd89905b05d09567203e8431849208b6ddc30f23e5fbe0c515fadaa
|
|
| MD5 |
e20d1def5e9f06e1583b1b79d24481fa
|
|
| BLAKE2b-256 |
6103040c926b346ea8af48704fd33d491c6996f76f5fd1713740f2f4e3dfd1f7
|
File details
Details for the file philiprehberger_run_parallel-0.1.4-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_run_parallel-0.1.4-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
91f1cd35d75b837d427c87607b1ab882e4c6b1be58e8f966aa1e5d20efe98dd7
|
|
| MD5 |
0afb829b28ef1a16dc326a3fc9e62618
|
|
| BLAKE2b-256 |
bfef8c97adbc6c2482d08c958cde29a0fc1d3a3e958344d749e1ac37460840a0
|