Last active
February 10, 2018 02:57
-
-
Save 0x0L/cf2e65c075932a9921d824e6574e038c to your computer and use it in GitHub Desktop.
parmap with monitoring
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
def _parmap(fun, q_in, q_out, init=None): | |
init() if init else None | |
while True: | |
i, x = q_in.get() | |
ret = fun(x) | |
q_out.put((i, ret)) | |
def parmap(fun, tasks, n=None, init=None): | |
from multiprocessing import Pool, Queue | |
from tqdm import tqdm | |
q_in, q_out = Queue(), Queue() | |
for item in enumerate(tasks): | |
q_in.put(item) | |
with Pool(n, _parmap, (fun, q_in, q_out, init)): | |
n = len(tasks) | |
ret = [None] * n | |
for _ in tqdm(range(n)): | |
i, x = q_out.get() | |
ret[i] = x | |
return ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment