Passing multiple parameters to pool.map() function in Python [duplicate]

You can use functools.partial for this (as you suspected): from functools import partial def target(lock, iterable_item): for item in iterable_item: # Do cool stuff if (… some condition here …): lock.acquire() # Write to stdout or logfile, etc. lock.release() def main(): iterable = [1, 2, 3, 4, 5] pool = multiprocessing.Pool() l = multiprocessing.Lock() func … Read more

multiprocessing.Pool() slower than just using ordinary functions

These problems usually boil down to the following: The function you are trying to parallelize doesn’t require enough CPU resources (i.e. CPU time) to rationalize parallelization! Sure, when you parallelize with multiprocessing.Pool(8), you theoretically (but not practically) could get a 8x speed up. However, keep in mind that this isn’t free – you gain this … Read more

Filling a queue and managing multiprocessing in python

You could use the blocking capabilities of queue to spawn multiple process at startup (using multiprocessing.Pool) and letting them sleep until some data are available on the queue to process. If your not familiar with that, you could try to “play” with that simple program: import multiprocessing import os import time the_queue = multiprocessing.Queue() def … Read more

How do you pass a Queue reference to a function managed by pool.map_async()?

The following code seems to work: import multiprocessing, time def task(args): count = args[0] queue = args[1] for i in xrange(count): queue.put(“%d mississippi” % i) return “Done” def main(): manager = multiprocessing.Manager() q = manager.Queue() pool = multiprocessing.Pool() result = pool.map_async(task, [(x, q) for x in range(10)]) time.sleep(1) while not q.empty(): print q.get() print result.get() … Read more

Can I use a multiprocessing Queue in a function called by Pool.imap?

The trick is to pass the Queue as an argument to the initializer. Appears to work with all the Pool dispatch methods. import multiprocessing as mp def f(x): f.q.put(‘Doing: ‘ + str(x)) return x*x def f_init(q): f.q = q def main(): jobs = range(1,6) q = mp.Queue() p = mp.Pool(None, f_init, [q]) results = p.imap(f, … Read more

Python Process Pool non-daemonic?

The multiprocessing.pool.Pool class creates the worker processes in its __init__ method, makes them daemonic and starts them, and it is not possible to re-set their daemon attribute to False before they are started (and afterwards it’s not allowed anymore). But you can create your own sub-class of multiprocesing.pool.Pool (multiprocessing.Pool is just a wrapper function) and … Read more

Keyboard Interrupts with python’s multiprocessing Pool

This is a Python bug. When waiting for a condition in threading.Condition.wait(), KeyboardInterrupt is never sent. Repro: import threading cond = threading.Condition(threading.Lock()) cond.acquire() cond.wait(None) print “done” The KeyboardInterrupt exception won’t be delivered until wait() returns, and it never returns, so the interrupt never happens. KeyboardInterrupt should almost certainly interrupt a condition wait. Note that this … Read more