在 Python 的多处理库中获取队列的长度 [英] Get length of Queue in Python's multiprocessing library
问题描述
我有一个 multiprocessing.Manager
对象,它包含一个 multiprocessing.Queue
来管理我希望一组进程完成的所有工作.我想获得此队列中剩余的元素数量,并想知道如何做到这一点?
I have a multiprocessing.Manager
object that contains a multiprocessing.Queue
to manage all of the work that I want a group of processes to do. I would like to get the number of elements left in this queue and was wondering how to do this?
Python 的内置 len()
函数不起作用.
Python's inbuilt len()
function does not work.
推荐答案
如果您所说的队列是 multiprocessing.Queue
,请尝试使用 qsize()
的方法multiprocessing.Queue
对象,但要小心:
If the queue you are talking about is multiprocessing.Queue
, try to use qsize()
method for multiprocessing.Queue
objects, but be careful:
qsize()
返回队列的大致大小.因为多线程/多处理语义,这个数字不可靠.
Return the approximate size of the queue. Because of multithreading/multiprocessing semantics, this number is not reliable.
请注意,这可能会在 Unix 平台上引发 NotImplementedError,例如未实现 sem_getvalue() 的 Mac OS X.
Note that this may raise NotImplementedError on Unix platforms like Mac OS X where sem_getvalue() is not implemented.
这篇关于在 Python 的多处理库中获取队列的长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!