【问题标题】:Python queues - number of task_done()Python 队列 - task_done() 的数量
【发布时间】:2015-05-31 05:55:00
【问题描述】:

我想知道多线程程序中队列完成的任务量(所有线程完成的总和)。找出这一点的最佳方法是什么? 我注意到:

  1. 我已阅读文档,似乎没有简单的方法 (https://docs.python.org/3.4/library/queue.html)。
  2. 队列类的 'join' 方法表明可以这样做,因为这指的是调用 task_done() 方法。
  3. “qsize”方法返回队列中当前未处理的项目数(据我了解) - 即与 task_done() 无关。
  4. 最好使用 python 3.4 的解决方案。

【问题讨论】:

    标签: python multithreading python-3.x queue


    【解决方案1】:

    您可以尝试扩展 Queue 类。类似的东西

    from queue import Queue
    
    class MyQueue(Queue):
    
        def __init__(self):
            #In py3, I believe you can just use super()
            #with no args
            super(MyQueue, self).__init__()
            self.completed_count = 0
    
        def task_done(self):
            self.completed_count += 1
            super(MyQueue, self).task_done()
    
        def get_task_count(self):
            return self.completed_count
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-08
      • 2011-11-28
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      • 2020-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多