<legend id='YJzrH'><style id='YJzrH'><dir id='YJzrH'><q id='YJzrH'></q></dir></style></legend>

    <small id='YJzrH'></small><noframes id='YJzrH'>

  • <i id='YJzrH'><tr id='YJzrH'><dt id='YJzrH'><q id='YJzrH'><span id='YJzrH'><b id='YJzrH'><form id='YJzrH'><ins id='YJzrH'></ins><ul id='YJzrH'></ul><sub id='YJzrH'></sub></form><legend id='YJzrH'></legend><bdo id='YJzrH'><pre id='YJzrH'><center id='YJzrH'></center></pre></bdo></b><th id='YJzrH'></th></span></q></dt></tr></i><div id='YJzrH'><tfoot id='YJzrH'></tfoot><dl id='YJzrH'><fieldset id='YJzrH'></fieldset></dl></div>
      <bdo id='YJzrH'></bdo><ul id='YJzrH'></ul>
        <tfoot id='YJzrH'></tfoot>

        在 Python 线程/队列方面需要一些帮助

        Need some assistance with Python threading/queue(在 Python 线程/队列方面需要一些帮助)
          • <i id='4w8xo'><tr id='4w8xo'><dt id='4w8xo'><q id='4w8xo'><span id='4w8xo'><b id='4w8xo'><form id='4w8xo'><ins id='4w8xo'></ins><ul id='4w8xo'></ul><sub id='4w8xo'></sub></form><legend id='4w8xo'></legend><bdo id='4w8xo'><pre id='4w8xo'><center id='4w8xo'></center></pre></bdo></b><th id='4w8xo'></th></span></q></dt></tr></i><div id='4w8xo'><tfoot id='4w8xo'></tfoot><dl id='4w8xo'><fieldset id='4w8xo'></fieldset></dl></div>
            • <bdo id='4w8xo'></bdo><ul id='4w8xo'></ul>

            • <small id='4w8xo'></small><noframes id='4w8xo'>

                <tbody id='4w8xo'></tbody>
                <legend id='4w8xo'><style id='4w8xo'><dir id='4w8xo'><q id='4w8xo'></q></dir></style></legend>

                <tfoot id='4w8xo'></tfoot>

                  本文介绍了在 Python 线程/队列方面需要一些帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  import threading
                  import Queue
                  import urllib2
                  import time
                  
                  class ThreadURL(threading.Thread):
                  
                      def __init__(self, queue):
                          threading.Thread.__init__(self)
                  
                          self.queue = queue
                  
                      def run(self):
                          while True:
                              host = self.queue.get()
                              sock = urllib2.urlopen(host)
                              data = sock.read()
                  
                              self.queue.task_done()
                  
                  hosts = ['http://www.google.com', 'http://www.yahoo.com', 'http://www.facebook.com', 'http://stackoverflow.com']
                  start = time.time()
                  
                  def main():
                      queue = Queue.Queue()
                  
                      for i in range(len(hosts)):
                          t = ThreadURL(queue)
                          t.start()
                  
                      for host in hosts:
                          queue.put(host)
                  
                      queue.join()
                  
                  if __name__ == '__main__':
                      main()
                      print 'Elapsed time: {0}'.format(time.time() - start)
                  

                  我一直在尝试了解如何执行线程,在学习了一些教程之后,我想出了上述内容.

                  I've been trying to get my head around how to perform Threading and after a few tutorials, I've come up with the above.

                  它应该做的是:

                  1. 初始化队列
                  2. 创建我的线程池,然后将主机列表排队
                  3. 一旦主机在队列中并读取网站数据,我的 ThreadURL 类就会开始工作
                  4. 程序应该完成

                  我首先想知道的是,我这样做是否正确?这是处理线程的最佳方式吗?

                  What I want to know first off is, am I doing this correctly? Is this the best way to handle threads?

                  其次,我的程序无法退出.它打印出 Elapsed time 行,然后挂在那里.我必须杀死我的终端才能让它消失.我假设这是由于我对 queue.join() 的错误使用?

                  Secondly, my program fails to exit. It prints out the Elapsed time line and then hangs there. I have to kill my terminal for it to go away. I'm assuming this is due to my incorrect use of queue.join() ?

                  推荐答案

                  你的代码看起来不错,也很干净.

                  Your code looks fine and is quite clean.

                  您的应用程序仍然挂起"的原因是工作线程仍在运行,等待主应用程序将某些内容放入队列中,即使您的主线程已完成.

                  The reason your application still "hangs" is that the worker threads are still running, waiting for the main application to put something in the queue, even though your main thread is finished.

                  解决此问题的最简单方法是将线程标记为守护进程,方法是在调用 start 之前执行 t.daemon = True.这样,线程就不会阻塞程序停止.

                  The simplest way to fix this is to mark the threads as daemons, by doing t.daemon = True before your call to start. This way, the threads will not block the program stopping.

                  这篇关于在 Python 线程/队列方面需要一些帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                  Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                  python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                  Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                  How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                  Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)
                    <tbody id='gJJw8'></tbody>

                  <i id='gJJw8'><tr id='gJJw8'><dt id='gJJw8'><q id='gJJw8'><span id='gJJw8'><b id='gJJw8'><form id='gJJw8'><ins id='gJJw8'></ins><ul id='gJJw8'></ul><sub id='gJJw8'></sub></form><legend id='gJJw8'></legend><bdo id='gJJw8'><pre id='gJJw8'><center id='gJJw8'></center></pre></bdo></b><th id='gJJw8'></th></span></q></dt></tr></i><div id='gJJw8'><tfoot id='gJJw8'></tfoot><dl id='gJJw8'><fieldset id='gJJw8'></fieldset></dl></div>

                    <small id='gJJw8'></small><noframes id='gJJw8'>

                    <legend id='gJJw8'><style id='gJJw8'><dir id='gJJw8'><q id='gJJw8'></q></dir></style></legend>

                      <tfoot id='gJJw8'></tfoot>
                          <bdo id='gJJw8'></bdo><ul id='gJJw8'></ul>