如何获得 multiprocessing.Queue 的最大值
How to get the maxsize of multiprocessing.Queue
与 queue.Queue 相反,multiprocessing.Queue 似乎没有 maxsize 属性。当我尝试通过调用 queue_object.maxsize
获取最大尺寸时,出现以下异常:AttributeError: 'Queue' object has no attribute 'maxsize'
.
这是我的实现错误还是实际上不存在?如果它不存在,是否有其他方式获取此信息?
如何重现:
Python 3.7.6 (v3.7.6:43364a7ae0, Dec 18 2019, 14:18:50)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from multiprocessing import Queue
>>> q = Queue(maxsize=10)
>>> q.maxsize
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Queue' object has no attribute 'maxsize'
编辑:
正如下面的答案中所解释的,这个属性不受支持,这让我想到为什么会这样?我相信 multiprocessing.Queue 是基于支持该属性的 queue.Queue(这首先导致了混淆)。
我找到了解决方法,在multiprocessing的source code中可以看到,属性设置为私有属性。因此,为了访问它,可以简单地执行 queue_object._maxsize
。这不是一个优雅的解决方案,我想知道为什么要这样实现。
Python 3.7.6 (v3.7.6:43364a7ae0, Dec 18 2019, 14:18:50)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from multiprocessing import Queue
>>> q = Queue(maxsize=10)
>>> q._maxsize
10
multiprocessing.Queue
复制 queue.Queue
的记录 API。它没有努力复制实施细节。
queue.Queue
实例的 maxsize
属性是未记录的实现细节。缺少前面的下划线并不能使它成为 public。用户从未打算与此属性进行交互。不能保证它会存在于所有 Python 实现中,或者它会存在于未来的版本中。 (例如,如果 queue
模块获得了 C 实现,则此属性很可能会消失。)
获取 multiprocessing.Queue
最大大小的预期方法是跟踪创建对象时传递的值。如果你决定直接从队列对象中取出它并且你可以访问实现细节(就像你在 queue.Queue
中使用 maxsize
所做的那样),你可以访问当前实现的私有属性使用:
queue._maxsize
不能保证这会在未来的版本或其他 Python 实现中起作用。
与 queue.Queue 相反,multiprocessing.Queue 似乎没有 maxsize 属性。当我尝试通过调用 queue_object.maxsize
获取最大尺寸时,出现以下异常:AttributeError: 'Queue' object has no attribute 'maxsize'
.
这是我的实现错误还是实际上不存在?如果它不存在,是否有其他方式获取此信息?
如何重现:
Python 3.7.6 (v3.7.6:43364a7ae0, Dec 18 2019, 14:18:50)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from multiprocessing import Queue
>>> q = Queue(maxsize=10)
>>> q.maxsize
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Queue' object has no attribute 'maxsize'
编辑: 正如下面的答案中所解释的,这个属性不受支持,这让我想到为什么会这样?我相信 multiprocessing.Queue 是基于支持该属性的 queue.Queue(这首先导致了混淆)。
我找到了解决方法,在multiprocessing的source code中可以看到,属性设置为私有属性。因此,为了访问它,可以简单地执行 queue_object._maxsize
。这不是一个优雅的解决方案,我想知道为什么要这样实现。
Python 3.7.6 (v3.7.6:43364a7ae0, Dec 18 2019, 14:18:50)
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from multiprocessing import Queue
>>> q = Queue(maxsize=10)
>>> q._maxsize
10
multiprocessing.Queue
复制 queue.Queue
的记录 API。它没有努力复制实施细节。
queue.Queue
实例的 maxsize
属性是未记录的实现细节。缺少前面的下划线并不能使它成为 public。用户从未打算与此属性进行交互。不能保证它会存在于所有 Python 实现中,或者它会存在于未来的版本中。 (例如,如果 queue
模块获得了 C 实现,则此属性很可能会消失。)
获取 multiprocessing.Queue
最大大小的预期方法是跟踪创建对象时传递的值。如果你决定直接从队列对象中取出它并且你可以访问实现细节(就像你在 queue.Queue
中使用 maxsize
所做的那样),你可以访问当前实现的私有属性使用:
queue._maxsize
不能保证这会在未来的版本或其他 Python 实现中起作用。