How do you make a worker process block while waiting for Value or Array from multiprocessing?

前端 未结 2 1231
离开以前
离开以前 2021-01-17 01:05

This document shows an example to share state between processes using Value and Array from multiprocessing library:

from multi

2条回答
  •  天命终不由人
    2021-01-17 01:45

    To communicate in a thread-safe manner you can use Queue. The get() method blocks if the queue is empty, and waits until a new element is put():

    from multiprocessing import Process, Queue
    
    def f(q):
        while True:
            element = q.get()
            print(element)
    
    if __name__ == '__main__':
        q = Queue()
        p = Process(target=f, args=(q,))
        p.start()
        q.put([42, None, 'hello'])
        p.join()
    

提交回复
热议问题