这个while循环如何退出?

Sha*_*kan 1 python multithreading while-loop

那么,当线程启动时,这段代码如何退出while语句?(请不要考虑缩进)

class ThreadUrl(threading.Thread):
    """Threaded Url Grab"""
    def __init__(self, queue, out_queue):
        threading.Thread.__init__(self)
        self.queue = queue
        self.out_queue = out_queue

    def run(self):
        while True:
            #grabs host from queue
            host = self.queue.get()

            #grabs urls of hosts and then grabs chunk of webpage
            url = urllib2.urlopen(host)
            chunk = url.read()

            #place chunk into out queue
            self.out_queue.put(chunk)

            #signals to queue job is done
            self.queue.task_done()
Run Code Online (Sandbox Code Playgroud)

**编辑*

启动线程的代码:

def main():

#spawn a pool of threads, and pass them queue instance
    for i in range(5):
        t = ThreadUrl(queue)
        t.setDaemon(True)
        t.start()

    queue.join()
Run Code Online (Sandbox Code Playgroud)

Dun*_*can 5

它不必退出while语句以终止代码.这里发生的一切都是线程已经消耗了队列中queue.join()返回的所有内容.

一旦queue.join()主代码中的调用返回主代码将退出,并且因为您将该线程标记为守护进程,整个应用程序将退出并且您的后台线程将被终止.