philiprehberger-run-parallel
Run multiple functions in parallel and collect results with the simplest possible API.
Installation
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]
Track progress
from philiprehberger_run_parallel import parallel
def on_progress(completed, total):
print(f"{completed}/{total} done")
results = parallel(
lambda: 1 + 1,
lambda: 2 + 2,
lambda: 3 + 3,
on_progress=on_progress,
)
# 1/3 done
# 2/3 done
# 3/3 done
Process-based parallelism
from philiprehberger_run_parallel import parallel_process
def cpu_work():
return sum(range(10_000_000))
results = parallel_process([cpu_work, cpu_work, cpu_work], max_workers=3)
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
| Function / Class | Description |
|---|---|
parallel(*tasks, timeout=None, on_progress=None) -> list |
Run callables or (fn, *args) tuples via ThreadPoolExecutor, return results in order. Optional progress callback (completed, total). |
parallel_process(fns, max_workers=None) -> list |
Run callables via ProcessPoolExecutor for CPU-bound work, 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
Support
If you find this project useful:
License
Release files for philiprehberger-run-parallel 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| philiprehberger_run_parallel-0.2.1.tar.gz | 6.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| philiprehberger_run_parallel-0.2.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 12.2 kB
Release files / philiprehberger_run_parallel-0.2.1.tar.gz
| Download URL | philiprehberger_run_parallel-0.2.1.tar.gz |
|---|---|
| Size | 6.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
8403d22ab2323b1debe7a8da588a99bf63e8cb7661df7957a36b16dc0f65279b
|
|
BLAKE2b-256 checksum How to use checksums |
d67fb70f74dcdec1ab8ef8bcc1c918a9bf2ca3231b09fa1425b950ea6342ab6e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|
Release files / philiprehberger_run_parallel-0.2.1-py3-none-any.whl
| Download URL | philiprehberger_run_parallel-0.2.1-py3-none-any.whl |
|---|---|
| Size | 5.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
48163fe58368f9e362b7775739ddae7037db999fc8677a94a4603e8df7e864a9
|
|
BLAKE2b-256 checksum How to use checksums |
0625ab160a4d453a95adf224c2dc052dd14fbb9cbf19bc8acb06174656f9a849
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.12.13
|