1
在下面的代碼中,如果我放入daemon = True
,consumer將在讀取所有隊列條目之前退出。如果消費者不是守護進程,那麼即使在所有條目的task_done()
之後,主線程也總是被阻塞。JoinableQueue join()方法甚至在task_done()之後阻塞主線程
from multiprocessing import Process, JoinableQueue
import time
def consumer(queue):
while True:
final = queue.get()
print (final)
queue.task_done()
def producer1(queue):
for i in "QWERTYUIOPASDFGHJKLZXCVBNM":
queue.put(i)
if __name__ == "__main__":
queue = JoinableQueue(maxsize=100)
p1 = Process(target=consumer, args=((queue),))
p2 = Process(target=producer1, args=((queue),))
#p1.daemon = True
p1.start()
p2.start()
print(p1.is_alive())
print (p2.is_alive())
for i in range(1, 10):
queue.put(i)
time.sleep(0.01)
queue.join()