Multiprocessing in a pipeline done right

后端 未结 6 1202
长情又很酷
长情又很酷 2021-02-04 05:06

I\'d like to know how multiprocessing is done right. Assuming I have a list [1,2,3,4,5] generated by function f1 which is written to a Queue

6条回答
  •  忘了有多久
    2021-02-04 05:52

    For Idea 1, how about:

    import multiprocessing as mp
    
    sentinel=None
    
    def f2(inq,outq):
        while True:
            val=inq.get()
            if val is sentinel:
                break
            outq.put(val*2)
    
    def f3(outq):
        while True:
            val=outq.get()
            if val is sentinel:
                break
            print(val)
    
    def f1():
        num_workers=2
        inq=mp.Queue()
        outq=mp.Queue()
        for i in range(5):
            inq.put(i)
        for i in range(num_workers):        
            inq.put(sentinel)
        workers=[mp.Process(target=f2,args=(inq,outq)) for i in range(2)]
        printer=mp.Process(target=f3,args=(outq,))
        for w in workers:
            w.start()
        printer.start()
        for w in workers:
            w.join()
        outq.put(sentinel)
        printer.join()
    
    if __name__=='__main__':
        f1()
    

    The only difference from the description of Idea 1 is that f2 breaks out of the while-loop when it receives the sentinel (thus terminating itself). f1 blocks until the workers are done (using w.join()) and then sends f3 the sentinel (signaling that it break out of its while-loop).

提交回复
热议问题