Multiprocessing Queue need Manager?

☆樱花仙子☆ 提交于 2019-12-08 10:23:01

问题


Why program A deadlock but program B runs fine?

Both uses a queue to pass result between main process and sub-processes. Both wait for all sub-processes to end before retrieving the results from the queue.

In the actual program, I need a queue to pass streams of results between steps. This is for working on results when the results are generated and before one of the step completes.

Changing the mp.queue in program A to a queue of a mp.Manager would fix the deadlock. But using manager seems to have performance penalty, because the manager is managed by the main process.

Program A (deadlocks without manager):

import multiprocessing as mp
def worker(q, i): 
    q.put(i)

if __name__ == "__main__":
    q = mp.Queue()

    # Start sub-processes
    p = mp.Pool()
    for i in range(4):
        p.apply_async(worker, args=(q, i)) 

    # wait till all workers complete their task.
    p.close()
    p.join()

    # Get results from queue
    for i in range(4):
        print(q.get())

Program B is my simplification of a program at (https://docs.python.org/3/howto/logging-cookbook.html) below the line "basis for code meeting your own specific requirements", and it doesn't use a manager ...

Program B:

import multiprocessing as mp
def worker(q, i):
    q.put(i)

if __name__  == "__main__":
    q = mp.Queue()

    # Start sub-processes
    ws = []
    for i in range(4):
        w = mp.Process(target=worker, args=(q, i))
        ws.append(w)
        w.start()

    # wait till all workers complete their task.
    for w in ws:
        w.join()

    # Get results from queue
    for i in range(4):
        print(q.get())

来源:https://stackoverflow.com/questions/45346814/multiprocessing-queue-need-manager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!