from queue import Queue from threading import Thread import time q = Queue() def add_to_queue(): for i in range(10): print('添加') q.put(i) time.sleep(0.01) def get_from_queue(): while True: print(q.get()) q.task_done() t1 = Thread(target=add_to_queue) #把當前進程設置為守護進程 # 什么是守護線程: 主線程結束了, 該子線程會自動結束 t1.setDaemon(True) t1.start() time.sleep(0.1) print(q.unfinished_tasks) t2 = Thread(target=get_from_queue) #把當前進程設置為守護進程 t2.setDaemon(True) t2.start() print(q.unfinished_tasks) # t1.join() # t2.join()# 由于t2對應任務是一個死循環, 永遠都不會結束 #讓主線等待隊列任務完成 q.join()
浙公網安備 33010602011771號