简体   繁体   English

为什么在Python中使用线程和队列时需要无限循环

[英]Why is infinite loop needed when using threading and a queue in Python

I'm trying to understand how to use threading and I came across this nice example at http://www.ibm.com/developerworks/aix/library/au-threadingpython/ 我正在尝试理解如何使用线程,我在http://www.ibm.com/developerworks/aix/library/au-threadingpython/上看到了这个很好的例子

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

      hosts = ["http://yahoo.com", "http://google.com", "http://amazon.com",
      "http://ibm.com", "http://apple.com"]

      queue = Queue.Queue()

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

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

            #grabs urls of hosts and prints first 1024 bytes of page
            url = urllib2.urlopen(host)
            print url.read(1024)

            #signals to queue job is done
            self.queue.task_done()

      start = time.time()
      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()

       #populate queue with data   
          for host in hosts:
            queue.put(host)

       #wait on the queue until everything has been processed     
       queue.join()

      main()
      print "Elapsed Time: %s" % (time.time() - start)

The part I don't understand is why the run method has an infinite loop: 我不明白的部分是run方法有无限循环的原因:

        def run(self):
          while True:
            ... etc ...

Just for laughs I ran the program without the loop and it looks like it runs fine! 只是为了笑,我没有循环运行程序,看起来它运行正常! So can someone explain why this loop is needed? 那么有人可以解释为什么需要这个循环吗? Also how is the loop exited as there is no break statement? 由于没有break语句,循环如何退出?

Do you want the thread to perform more than one job? 你想让线程执行多个工作吗? If not, you don't need the loop. 如果没有,您不需要循环。 If so, you need something that's going to make it do that. 如果是这样,你需要一些能够做到这一点的东西。 A loop is a common solution. 循环是一种常见的解决方案。 Your sample data contains five job, and the program starts five threads. 您的示例数据包含五个作业,程序启动五个线程。 So you don't need any thread to do more than one job here. 所以,你不需要任何线程在这里做一个以上的作业。 Try adding one more URL to your workload, though, and see what changes. 但是,请尝试向工作负载添加一个URL,并查看更改内容。

The loop is required as without it each worker thread terminates as soon as it completes its first task. 循环是必需的,因为没有它,每个工作线程一完成第一个任务就会终止。 What you want is to have the worker take another task when it finishes. 你想要的是让工人完成另一项任务。

In the code above, you create 5 worker threads, which just happens to be sufficient to cover the 5 URL's you are working with. 在上面的代码中,您创建了5个工作线程,这恰好足以覆盖您正在使用的5个URL。 If you had >5 URL's you would find only the first 5 were processed. 如果你有> 5个URL,你会发现只有前5个被处理过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM