在 Python 的多处理库中获取队列的长度

Get length of Queue in Python's multiprocessing library

我有一个 multiprocessing.Manager 对象,其中包含一个 multiprocessing.Queue 来管理我希望一组进程完成的所有工作。我想获取此队列中剩余的元素数量,并且想知道如何执行此操作?

Python 的内置 len() 功能不起作用。

如果你说的队列是multiprocessing.Queue,尝试对multiprocessing.Queue个对象使用qsize()方法,但要小心:

qsize()

Return the approximate size of the queue. Because of multithreading/multiprocessing semantics, this number is not reliable.

Note that this may raise NotImplementedError on Unix platforms like Mac OS X where sem_getvalue() is not implemented.