Skip to content

Instantly share code, notes, and snippets.

@pankaj28843
Created October 10, 2014 03:22
Show Gist options
  • Select an option

  • Save pankaj28843/e0f91ca6a2142aa6c39c to your computer and use it in GitHub Desktop.

Select an option

Save pankaj28843/e0f91ca6a2142aa6c39c to your computer and use it in GitHub Desktop.
# Standard Library
import multiprocessing
DEFAULT_PROCESSES_COUNT = int(round(1.5 * multiprocessing.cpu_count()))
def spawn(f):
def fun(q_in, q_out):
while True:
i, x = q_in.get()
if i is None:
break
q_out.put((i, f(x)))
return fun
def parmap(f, X, nprocs=DEFAULT_PROCESSES_COUNT):
q_in = multiprocessing.Queue(1)
q_out = multiprocessing.Queue()
proc = [multiprocessing.Process(target=spawn(f), args=(q_in, q_out))
for _ in range(nprocs)]
for p in proc:
p.daemon = True
p.start()
sent = [q_in.put((i, x)) for i, x in enumerate(X)]
[q_in.put((None, None)) for _ in range(nprocs)]
res = [q_out.get() for _ in range(len(sent))]
[p.join() for p in proc]
return [x for i, x in sorted(res)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment