Python Queue.join()

Muh*_*man 4 python queue

即使我没有将线程设置为守护进程,一旦queue.join()完成和解除阻塞,程序是否应该退出?

#!/usr/bin/python
import Queue
import threading
import time

class workerthread(threading.Thread):
        def __init__(self,queue):
                threading.Thread.__init__(self)
                self.queue=queue
        def run(self):
                print 'In Worker Class'
                while True:
                        counter=self.queue.get()
                        print 'Going to Sleep'
                        time.sleep(counter)
                        print ' I am up!'
                        self.queue.task_done()
queue=Queue.Queue()

for i in range(10):
        worker=workerthread(queue)
        print 'Going to Thread!'
        worker.daemon=True
        worker.start()
for j in range(10):
        queue.put(j)
queue.join()
Run Code Online (Sandbox Code Playgroud)

任何帮助将非常感谢!

NPE*_*NPE 13

当您调用queue.join()主线程时,它所做的只是阻塞主线程,直到工作者处理了队列中的所有内容.它不会停止继续执行无限循环的工作线程.

如果工作线程是非deamon,它们的继续执行会阻止程序停止,而不管主线程是否已完成.