How can I recover the return value of a function passed to multiprocessing.Process?
Use shared variable to communicate. For example like this:
import multiprocessingdef worker(procnum, return_dict): """worker function""" print(str(procnum) + " represent!") return_dict[procnum] = procnumif __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() print(return_dict.values())
I think the approach suggested by @sega_sai is the better one. But it really needs a code example, so here goes:
import multiprocessingfrom os import getpiddef worker(procnum): print('I am number %d in process %d' % (procnum, getpid())) return getpid()if __name__ == '__main__': pool = multiprocessing.Pool(processes = 3) print(pool.map(worker, range(5)))
Which will print the return values:
I am number 0 in process 19139I am number 1 in process 19138I am number 2 in process 19140I am number 3 in process 19139I am number 4 in process 19140[19139, 19138, 19140, 19139, 19140]
If you are familiar with map
(the Python 2 built-in) this should not be too challenging. Otherwise have a look at sega_Sai's link.
Note how little code is needed. (Also note how processes are re-used).
For anyone else who is seeking how to get a value from a Process
using Queue
:
import multiprocessingret = {'foo': False}def worker(queue): ret = queue.get() ret['foo'] = True queue.put(ret)if __name__ == '__main__': queue = multiprocessing.Queue() queue.put(ret) p = multiprocessing.Process(target=worker, args=(queue,)) p.start() p.join() print(queue.get()) # Prints {"foo": True}
Note that in Windows or Jupyter Notebook, with multithreading
you have to save this as a file and execute the file. If you do it in a command prompt you will see an error like this:
AttributeError: Can't get attribute 'worker' on <module '__main__' (built-in)>