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

前端 未结 12 1837
野的像风
野的像风 2020-11-22 07:36

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?

12条回答
  •  爱一瞬间的悲伤
    2020-11-22 08:16

    This example shows how to use a list of multiprocessing.Pipe instances to return strings from an arbitrary number of processes:

    import multiprocessing
    
    def worker(procnum, send_end):
        '''worker function'''
        result = str(procnum) + ' represent!'
        print result
        send_end.send(result)
    
    def main():
        jobs = []
        pipe_list = []
        for i in range(5):
            recv_end, send_end = multiprocessing.Pipe(False)
            p = multiprocessing.Process(target=worker, args=(i, send_end))
            jobs.append(p)
            pipe_list.append(recv_end)
            p.start()
    
        for proc in jobs:
            proc.join()
        result_list = [x.recv() for x in pipe_list]
        print result_list
    
    if __name__ == '__main__':
        main()
    

    Output:

    0 represent!
    1 represent!
    2 represent!
    3 represent!
    4 represent!
    ['0 represent!', '1 represent!', '2 represent!', '3 represent!', '4 represent!']
    

    This solution uses fewer resources than a multiprocessing.Queue which uses

    • a Pipe
    • at least one Lock
    • a buffer
    • a thread

    or a multiprocessing.SimpleQueue which uses

    • a Pipe
    • at least one Lock

    It is very instructive to look at the source for each of these types.

提交回复
热议问题