Python wrappers for easy multiprocessing and threading
Project description
parallel-execute
Lightweight Python wrappers for easy multiprocessing and multithreading.
Run multiple functions in parallel using a simple API built on top of threading or multiprocessing. Supports error handling, result tracking, timeouts, and controlled concurrency.
Installation
Install the package using pip:
pip install parallel-execute
Usage Example
1. Create a Loom
A Loom takes multiple tasks (functions) and executes them in parallel using either threads or processes.
Using Threads:
from pexecute.thread import ThreadLoom
loom = ThreadLoom(max_runner_cap=10)
Using Processes:
from pexecute.process import ProcessLoom
loom = ProcessLoom(max_runner_cap=10)
max_runner_cap: The maximum number of threads/processes to run in parallel. You can queue as many functions as needed; only max_runner_cap will run at the same time.
2. Add Tasks to the Loom
Add a Single Task:
Use add_function to add individual functions:
loom.add_function(f1, args1, kw1)
loom.add_function(f2, args2, kw2)
loom.add_function(f3, args3, kw3)
Add Multiple Tasks at Once:
Use add_work to add a batch of functions:
work = [(f1, args1, kwargs1), (f2, args2, kwargs2), (f3, args3, kwargs3)]
loom.add_work(work)
3. Execute Tasks
Once all tasks are added, call execute() to run them. It returns a dictionary mapping each task to its result.
output = loom.execute()
By default, the keys are integers in the order the functions were added. Each value is a dictionary containing the result and execution metadata.
Example:
def fun1():
return "Hello World"
def fun2(a):
return a
def fun3(a, b=0):
return a + b
loom.add_function(fun1, [], {})
loom.add_function(fun2, [1], {})
loom.add_function(fun3, [1], {'b': 3})
output = loom.execute()
>>> output
{
0: {
'output': 'Hello World',
'got_error': False,
'error': None,
'started_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 395002),
'finished_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 396500),
'execution_time': 0.001498,
},
1: {
'output': 1,
'got_error': False,
'error': None,
'started_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 396590),
'finished_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 397651),
'execution_time': 0.001061
},
2: {
'output': 4,
'got_error': False,
'error': None,
'started_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 400323),
'finished_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 401749),
'execution_time': 0.001426
}
}
4. Using Custom Keys
You can assign a custom key to each task. This allows you to identify results more explicitly in the output dictionary.
loom.add_function(fun1, [], {}, 'key1')
loom.add_function(fun2, [1], {}, 'fun2')
loom.add_function(fun3, [1], {'b': 3}, 'xyz')
output = loom.execute()
>>> output
{
'key1': {
'output': 'Hello World',
'got_error': False,
'error': None,
'started_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 395002),
'finished_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 396500),
'execution_time': 0.001498,
},
'fun2': {
'output': 1,
'got_error': False,
'error': None,
'started_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 396590),
'finished_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 397651),
'execution_time': 0.001061
},
'xyz': {
'output': 4,
'got_error': False,
'error': None,
'started_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 400323),
'finished_time': datetime.datetime(2019, 6, 28, 19, 44, 58, 401749),
'execution_time': 0.001426
}
}
Migration Notice
parallel-execute is now powered by a newer, more powerful backend called concurra.
New users are encouraged to switch to the new interface:
from concurra import TaskRunner
runner = TaskRunner()
runner.add_task(my_func, *args, **kwargs)
results = runner.run()
Backward compatibility with ThreadLoom and ProcessLoom is currently maintained, but may be deprecated in future versions.
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 parallel_execute-2.0.3.tar.gz.
File metadata
- Download URL: parallel_execute-2.0.3.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a8deae6233b657733036ea8632a7a7525a24169d90a94da533d64f7bdf157276
|
|
| MD5 |
a3db245638ef6529ee5fa27e1014c7f2
|
|
| BLAKE2b-256 |
31c2c5643d1ed5ce6f867566db55ca9a4a925692e3b31b4310f4f9bcea9185fa
|
File details
Details for the file parallel_execute-2.0.3-py3-none-any.whl.
File metadata
- Download URL: parallel_execute-2.0.3-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
57e574fe5f1d7c7879375712087beb4ce3de6282ed84044fb15eeaa523854d2c
|
|
| MD5 |
603c883662c2cf9b40963a07a2383d23
|
|
| BLAKE2b-256 |
00f2d4ca09aa00eb778c75cb4e90cd42e56f5af98cf22221e19aaea7f8c801e0
|