Elegant multiprocessing without the boilerplate and confusing syntax
Project description
multiprocessing-wrap
A simple interface for writing concurrent scripts. Get the most out of multiprocessing
without all the boilerplate and confusing syntax!
Features
- Sensible error propagation - having a stack trace showing where your code speeds debugging and development
- Built-in loading bar as default using tqdm
- Uses dill for pickling, which extends the types that can be passed to your workers (see here for documentation of the limitations of python's default pickling)
Installation
To install using pip:
pip install multiprocessing-wrap
Usage
You can use the functional multiprocess
for one line multiprocessing:
from multiprocess import multiprocess
f = lambda: print(1)
multiprocess(f, [(), (), ()])
1 1 1
Otherwise you can use the Multiprocess
class to use the more explicit add_tasks
and do_tasks
directives.:
from multiprocess import Multiprocess
m = Multiprocess(show_loading_bar=False)
f = lambda: print(1)
m.add_tasks(f, [(), (), ()])
m.do_tasks() # blocking
m.close()
A more involved example of sorting numbers using sleep
. Note that you only have as many workers as you have threads, so if you have 4 threads you will only be able to sort up to 4 numbers with this approach:
from multiprocess import multiprocess, Queue
from time import sleep
def sleep_sort():
q = Queue()
def f(q, x):
sleep(x)
q.push(x)
multiprocess(f, [(q, 1,), (q, 2,), (q, 1.5,)])
print('SORTED')
while not q.empty():
print(q.pop())
sleep_sort()
1 1.5 2
Error handling
Errors from within a process are propagated back to the parent with stack information. For example:
from multiprocess import Multiprocess
m = Multiprocess()
def f(x):
raise ValueError('bad error')
m.add_tasks(f, [(1,)])
m.do_tasks()
m.close()
Traceback (most recent call last): File "./example3.py", line 9, in <module> m.do_tasks() File "/Users/dom/Documents/git/Multiprocess/src/multiprocess.py", line 53, in do_tasks self._check_for_exceptions() File "/Users/dom/Documents/git/Multiprocess/src/multiprocess.py", line 71, in _check_for_exceptions "\n".join(['ERROR: ' + str(e) for e in exceptions])) multiprocess.MultiprocessException: 1 errors occurred: ERROR: Error in function call "f((1,))" Traceback (most recent call last): File "/Users/dom/Documents/git/Multiprocess/src/multiprocess.py", line 85, in my_worker fn(*rem_args) File "./example3.py", line 6, in f raise ValueError('bad error') ValueError: bad error
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
File details
Details for the file multiprocessing_wrap-0.0.2.tar.gz
.
File metadata
- Download URL: multiprocessing_wrap-0.0.2.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4329a6207303e61f54627b6d4fbdb7a9de693146573ed82f48872744dea86263 |
|
MD5 | 37638c792b30bc716d879e19b97a0626 |
|
BLAKE2b-256 | 516e36baca7e2b08108aa796a86f6224b45a5b81fbb6af8e6c0e8bb752553a37 |
File details
Details for the file multiprocessing_wrap-0.0.2-py3-none-any.whl
.
File metadata
- Download URL: multiprocessing_wrap-0.0.2-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3dc85bcdbd6080180137e7682b4943da27dd345389b2ce9a2e34fd9c03f43983 |
|
MD5 | c44f380fa764a2c4e54dea2dec2fd5a9 |
|
BLAKE2b-256 | 91c178f87a016b8662efcd652f5fe1547a76ff6afbc24b7c1d09cf5b99f04217 |