How many processes should I run in parallel?

Always separate the number of processes from the number of tasks. There’s no reason why the two should be identical, and by making the number of processes a variable, you can experiment to see what works well for your particular problem. No theoretical answer is as good as old-fashioned get-your-hands-dirty benchmarking with real data. Here’s … Read more

python Pool with worker Processes

I would suggest that you use a Queue for this. class Worker(Process): def __init__(self, queue): super(Worker, self).__init__() self.queue = queue def run(self): print(‘Worker started’) # do some initialization here print(‘Computing things!’) for data in iter(self.queue.get, None): # Use data Now you can start a pile of these, all getting work from a single queue request_queue … Read more

python subclassing multiprocessing.Process

Subclassing multiprocessing.Process: However I cannot get back the values, how can I use queues in this way? Process needs a Queue() to receive the results… An example of how to subclass multiprocessing.Process follows… from multiprocessing import Process, Queue class Processor(Process): def __init__(self, queue, idx, **kwargs): super(Processor, self).__init__() self.queue = queue self.idx = idx self.kwargs = … Read more

Python Multiprocessing Locks

If you change pool.apply_async to pool.apply, you get this exception: Traceback (most recent call last): File “p.py”, line 15, in <module> pool.apply(job, [l, i]) File “/usr/lib/python2.7/multiprocessing/pool.py”, line 244, in apply return self.apply_async(func, args, kwds).get() File “/usr/lib/python2.7/multiprocessing/pool.py”, line 558, in get raise self._value RuntimeError: Lock objects should only be shared between processes through inheritance pool.apply_async is … Read more

How to solve memory issues while multiprocessing using Pool.map()?

Prerequisite In Python (in the following I use 64-bit build of Python 3.6.5) everything is an object. This has its overhead and with getsizeof we can see exactly the size of an object in bytes: >>> import sys >>> sys.getsizeof(42) 28 >>> sys.getsizeof(‘T’) 50 When fork system call used (default on *nix, see multiprocessing.get_start_method()) to … Read more

How can I get the return value of a function passed to multiprocessing.Process?

Use a shared variable to communicate. For example, like this, Example Code: import multiprocessing def worker(procnum, return_dict): “””worker function””” print(str(procnum) + ” represent!”) return_dict[procnum] = procnum if __name__ == “__main__”: manager = multiprocessing.Manager() return_dict = manager.dict() jobs = [] for i in range(5): p = multiprocessing.Process(target=worker, args=(i, return_dict)) jobs.append(p) p.start() for proc in jobs: proc.join() … Read more

How does the callback function work in multiprocessing map_async?

Callback is called once with the result ([[0], [0, 1]]) if you use map_async. >>> from multiprocessing import Pool >>> def myfunc(x): … return [i for i in range(x)] … >>> A = [] >>> def mycallback(x): … print(‘mycallback is called with {}’.format(x)) … A.extend(x) … >>> pool=Pool() >>> r = pool.map_async(myfunc, (1,2), callback=mycallback) >>> … Read more