blz blz - 1 month ago 8
Python Question

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

In the example code below, I'd like to recover the return value of the function

worker
. How can I go about doing this? Where is this value stored?

Example Code:

import multiprocessing

def worker(procnum):
'''worker function'''
print str(procnum) + ' represent!'
return procnum


if __name__ == '__main__':
jobs = []
for i in range(5):
p = multiprocessing.Process(target=worker, args=(i,))
jobs.append(p)
p.start()

for proc in jobs:
proc.join()
print jobs


Output:

0 represent!
1 represent!
2 represent!
3 represent!
4 represent!
[<Process(Process-1, stopped)>, <Process(Process-2, stopped)>, <Process(Process-3, stopped)>, <Process(Process-4, stopped)>, <Process(Process-5, stopped)>]


I can't seem to find the relevant attribute in the objects stored in
jobs
.

Thanks in advance,
blz

Answer

Use shared variable to communicate. For example like this:

def worker(procnum, return_dict):
    '''worker function'''
    print str(procnum) + ' represent!'
    return_dict[procnum] = procnum


if __name__ == '__main__':
    manager = 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()
Comments