简体   繁体   English

Python队列-task_done()的数量

[英]Python queues - number of task_done()

I wish to know the amount of tasks completed (the sum completed by all threads) for a queue in a multi-threaded program. 我希望知道多线程程序中队列的已完成任务数量(所有线程已完成的总和)。 What is the best way to find this out? 找出答案的最佳方法是什么? I note that: 我注意到:

  1. I've read the documentation and there doesn't appear to be a simple method ( https://docs.python.org/3.4/library/queue.html ). 我已经阅读了文档,似乎没有一个简单的方法( https://docs.python.org/3.4/library/queue.html )。
  2. The 'join' method of the queue class suggests that this can be done, as this refers to calling of the task_done() method. 队列类的“ join”方法表明可以做到这一点,因为它涉及对task_done()方法的调用。
  3. The 'qsize' method returns the number of items in the queue that are currently unprocessed (as I understand it) - ie does not have a relation to task_done(). “ qsize”方法返回队列中当前未处理的项目数(据我了解)-即与task_done()没有关系。
  4. Solution for python 3.4 is preferable. 最好使用python 3.4的解决方案。

You could try extending the Queue class. 您可以尝试扩展Queue类。 Something like 就像是

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

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

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