如果我们将 maxsize = 0 放入 Queue 。然后会发生什么,我们可以在此之后插入元素吗?

If we put maxsize = 0 in Queue . What happens then and can we insert elements after this?

如果 maxsize = 0,我们可以在队列中插入元素吗? 这是什么意思:- 'A max size of zero ‘0’ means an infinite queue'? 如果我们将 maxsize= 0 它怎么会有无限队列?

这正是 documentation 所说的。

Constructor for a FIFO queue. maxsize is an integer that sets the upperbound limit on the number of items that can be placed in the queue. Insertion will block once this size has been reached, until queue items are consumed. If maxsize is less than or equal to zero, the queue size is infinite.

如果问题是“我们如何才能拥有最大大小为 0 的无限队列”,那么答案很简单。 queue 包的作者决定这是他们用来表示无限队列的值。我不认为(也不知道)有更好的解释。

更直接的回答问题——队列确实是无限的:

>>> from queue import Queue
>>> queue = Queue(maxsize=0)

>>> for i in range(100000000):
...     queue.put(i)

即使您无法真正测试“无穷大”,此代码也会 运行 范围的末尾。

当文档说 “队列大小是无限的” 时,这并不意味着已经为队列分配了无限的内存,也不意味着您可以以某种方式添加一个无限的内存它的元素数量。这当然是不可能的,因为总是存在有限的内存限制。

当您向构造函数提供 0 时,Queue 实现本身 不会对您可以添加到队列中的项目数量设置任何限制。在向其中添加项目时,它将根据需要动态分配更多内存。它永远不会像在非零限制的情况下那样“阻止”插入。唯一的限制是可用内存。