Last active
May 28, 2017 13:53
-
-
Save igorcoding/8fe197a688473ce6f6166cd4604bd026 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import multiprocessing | |
def map_parallel(func, iterable, chunksize=1, *, | |
pool=None, | |
n_processes=None, | |
progress_bar_func=None, | |
pool_kwargs=None, | |
progress_bar_func_kwargs=None, | |
total=None | |
): | |
if pool_kwargs is None: | |
pool_kwargs = {} | |
if progress_bar_func_kwargs is None: | |
progress_bar_func_kwargs = {} | |
own_pool = False | |
if pool is None: | |
pool = multiprocessing.Pool(n_processes, **pool_kwargs) | |
own_pool = True | |
try: | |
if not callable(progress_bar_func): | |
return pool.map(func, iterable, chunksize) | |
else: | |
pool_iter = pool.imap(func, iterable, chunksize) | |
return [item for item in | |
progress_bar_func(pool_iter, | |
total=total or len(iterable), | |
**progress_bar_func_kwargs)] | |
finally: | |
if own_pool: | |
pool.terminate() | |
if __name__ == '__main__': | |
import time | |
import sys | |
import numpy as np | |
from tqdm import tqdm | |
def func(arg1): | |
time.sleep(1.0) | |
return arg1 * arg1 | |
arr = np.arange(0, 10) | |
start_time = time.time() | |
# res = [func(el) for el in arr] | |
res = map_parallel(func, arr, n_processes=4, chunksize=1, | |
progress_bar_func=tqdm, | |
progress_bar_func_kwargs=dict(desc='myfunc')) | |
print('{}'.format(res), file=sys.stderr) | |
print('Elapsed: {}'.format(time.time() - start_time), file=sys.stderr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment