queue
— A synchronized queue class同步队列类¶
Source code: Lib/queue.py
The queue
module implements multi-producer, multi-consumer queues. queue
模块实现多生产者、多消费者队列。It is especially useful in threaded programming when information must be exchanged safely between multiple threads. 当信息必须在多个线程之间安全交换时,它在线程编程中特别有用。The 该模块中的Queue
class in this module implements all the required locking semantics.Queue
类实现了所有必需的锁定语义。
The module implements three types of queue, which differ only in the order in which the entries are retrieved. 该模块实现了三种类型的队列,它们的不同之处仅在于检索条目的顺序。In a FIFO queue, the first tasks added are the first retrieved. 在FIFO队列中,第一个添加的任务是第一个检索到的任务。In a LIFO queue, the most recently added entry is the first retrieved (operating like a stack). 在LIFO队列中,最近添加的条目是第一个检索到的条目(像堆栈一样操作)。With a priority queue, the entries are kept sorted (using the 使用优先级队列,条目保持排序(使用heapq
module) and the lowest valued entry is retrieved first.heapq
模块),并首先检索值最低的条目。
Internally, those three types of queues use locks to temporarily block competing threads; however, they are not designed to handle reentrancy within a thread.在内部,这三种类型的队列使用锁来临时阻止竞争线程;然而,它们并不是为了处理线程中的可重入性而设计的。
In addition, the module implements a “simple” FIFO queue type, 此外,该模块实现了一种“简单”的FIFO队列类型SimpleQueue
, whose specific implementation provides additional guarantees in exchange for the smaller functionality.SimpleQueue
,其特定实现提供了额外的保证,以换取较小的功能。
The queue
module defines the following classes and exceptions:queue
模块定义以下类和异常:
-
class
queue.
Queue
(maxsize=0)¶ 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.FIFO队列的构造函数。maxsize是一个整数,用于设置队列中可放置项目数的上限。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.一旦达到此大小,插入将被阻止,直到队列项目被消耗。如果maxsize小于或等于零,则队列大小是无限的。
-
class
queue.
LifoQueue
(maxsize=0)¶ Constructor for a LIFO queue. maxsize is an integer that sets the upperbound limit on the number of items that can be placed in the queue.LIFO队列的构造函数。maxsize是一个整数,用于设置队列中可放置项目数的上限。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.如果maxsize小于或等于零,则队列大小是无限的。
-
class
queue.
PriorityQueue
(maxsize=0)¶ Constructor for a priority queue. maxsize is an integer that sets the upperbound limit on the number of items that can be placed in the queue.优先级队列的构造函数。maxsize是一个整数,用于设置队列中可放置项目数的上限。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.一旦达到此大小,插入将被阻止,直到队列项目被消耗。如果maxsize小于或等于零,则队列大小是无限的。The lowest valued entries are retrieved first (the lowest valued entry is the one returned by首先检索值最低的条目(值最低的条目的排序sorted(list(entries))[0]
).sorted(list(entries))[0]
返回的条目)。A typical pattern for entries is a tuple in the form:条目的典型模式是形式为(priority_number, data)
.(priority_number, data)
的元组。If the data elements are not comparable, the data can be wrapped in a class that ignores the data item and only compares the priority number:如果data元素不可比较,则可以将数据包装在一个类中,该类忽略数据项,只比较优先级:from dataclasses import dataclass, field
from typing import Any
@dataclass(order=True)
class PrioritizedItem:
priority: int
item: Any=field(compare=False)
-
class
queue.
SimpleQueue
¶ Constructor for an unbounded FIFO queue.无界FIFO队列的构造函数。Simple queues lack advanced functionality such as task tracking.简单队列缺少高级功能,如任务跟踪。New in version 3.7.版本3.7中新增。
-
exception
queue.
Empty
¶ Exception raised when non-blocking对空的get()
(orget_nowait()
) is called on aQueue
object which is empty.Queue
对象调用非阻塞的get()
(或get_nowait()
)时引发异常。
-
exception
queue.
Full
¶ Exception raised when non-blocking对已满的put()
(orput_nowait()
) is called on aQueue
object which is full.Queue
对象调用非阻塞put()
(或put_nowait()
)时引发异常。
Queue ObjectsQueue对象¶
Queue objects (队列对象(Queue
, LifoQueue
, or PriorityQueue
) provide the public methods described below.Queue
、LifoQueue
或PriorityQueue
)提供下面描述的公共方法。
-
Queue.
qsize
()¶ Return the approximate size of the queue.返回队列的大致大小。Note, qsize() > 0 doesn’t guarantee that a subsequent get() will not block, nor will qsize() < maxsize guarantee that put() will not block.请注意,qsize()
>0不能保证后续的get()
不会被阻塞,qsize()
<maxsize也不能保证put()
不会阻塞。
-
Queue.
empty
()¶ Return如果队列为空,则返回True
if the queue is empty,False
otherwise.True
,否则返回False
。If empty() returns如果True
it doesn’t guarantee that a subsequent call to put() will not block.empty()
返回True
,则不能保证后续对put()
的调用不会被阻塞。Similarly, if empty() returns类似地,如果False
it doesn’t guarantee that a subsequent call to get() will not block.empty()
返回False
,则不能保证后续对get()
的调用不会被阻塞。
-
Queue.
full
()¶ Return如果队列已满,则返回True
if the queue is full,False
otherwise.True
,否则返回False
。If full() returns如果True
it doesn’t guarantee that a subsequent call to get() will not block.full()
返回True,则不能保证后续对get()
的调用不会被阻塞。Similarly, if full() returns类似地,如果False
it doesn’t guarantee that a subsequent call to put() will not block.full()
返回False
,则不能保证后续对put()
的调用不会被阻塞。
-
Queue.
put
(item, block=True, timeout=None)¶ Put item into the queue.将item放入队列。If optional args block is true and timeout is如果可选参数block为None
(the default), block if necessary until a free slot is available.true
并且timeout为None
(默认值),则根据需要进行块,直到有可用插槽为止。If timeout is a positive number, it blocks at most timeout seconds and raises the如果timeout是一个正数,它最多会阻止timeout秒数,如果在该时间内没有可用插槽,则会引发Full
exception if no free slot was available within that time.Full
异常。Otherwise (block is false), put an item on the queue if a free slot is immediately available, else raise the否则(block为Full
exception (timeout is ignored in that case).false
),如果空闲插槽立即可用,则将项目放入队列,否则引发Full
异常(在这种情况下会忽略超时)。
-
Queue.
put_nowait
(item)¶ Equivalent to等效于put(item, block=False)
.put(item, block=False)
。
-
Queue.
get
(block=True, timeout=None)¶ Remove and return an item from the queue.从队列中删除并返回项目。If optional args block is true and timeout is如果可选参数block为None
(the default), block if necessary until an item is available.true
,timeout为None
(默认值),则根据需要进行块,直到项目可用。If timeout is a positive number, it blocks at most timeout seconds and raises the如果timeout为正数,则它最多阻止timeout秒数,如果在该时间内没有项目可用,则引发Empty
exception if no item was available within that time.Empty
异常。Otherwise (block is false), return an item if one is immediately available, else raise the否则(block为Empty
exception (timeout is ignored in that case).false
),如果项目立即可用,则返回一个项目,否则引发Empty
异常(在这种情况下会忽略timeout)。Prior to 3.0 on POSIX systems, and for all versions on Windows, if block is true and timeout is在POSIX系统上的3.0之前,对于Windows上的所有版本,如果block为None
, this operation goes into an uninterruptible wait on an underlying lock.true
并且timeout为None
,则此操作将进入底层锁上的不间断等待。This means that no exceptions can occur, and in particular a SIGINT will not trigger a这意味着不会发生异常,特别是SIGINT不会触发KeyboardInterrupt
.KeyboardInterrupt
。
-
Queue.
get_nowait
()¶ Equivalent to相当于get(False)
.get(False)
。
Two methods are offered to support tracking whether enqueued tasks have been fully processed by daemon consumer threads.提供了两种方法来支持跟踪已排队的任务是否已由守护进程使用者线程完全处理。
-
Queue.
task_done
()¶ Indicate that a formerly enqueued task is complete. Used by queue consumer threads.指示以前排队的任务已完成。由队列使用者线程使用。For each对于用于获取任务的每个get()
used to fetch a task, a subsequent call totask_done()
tells the queue that the processing on the task is complete.get()
,对task_done()
的后续调用告诉队列任务的处理已完成。If a如果join()
is currently blocking, it will resume when all items have been processed (meaning that atask_done()
call was received for every item that had beenput()
into the queue).join()
当前正在阻塞,则在处理完所有项目后,它将恢复(这意味着为已put()
入队列的每个项目都收到task_done()
调用)。Raises a如果调用次数超过队列中放置的项目数,则引发ValueError
if called more times than there were items placed in the queue.ValueError
。
-
Queue.
join
()¶ Blocks until all items in the queue have been gotten and processed.阻止,直到队列中的所有项目都已获取和处理。The count of unfinished tasks goes up whenever an item is added to the queue.每当将项目添加到队列中时,未完成任务的计数就会上升。The count goes down whenever a consumer thread calls每当使用者线程调用task_done()
to indicate that the item was retrieved and all work on it is complete.task_done()
来指示已检索到该项并且所有工作都已完成时,计数就会递减。When the count of unfinished tasks drops to zero,当未完成任务的计数降至零时,join()
unblocks.join()
将取消阻止。
Example of how to wait for enqueued tasks to be completed:如何等待排队任务完成的示例:
import threading, queue
q = queue.Queue()
def worker():
while True:
item = q.get()
print(f'Working on {item}')
print(f'Finished {item}')
q.task_done()
# Turn-on the worker thread.
threading.Thread(target=worker, daemon=True).start()
# Send thirty task requests to the worker.
for item in range(30):
q.put(item)
# Block until all tasks are done.
q.join()
print('All work completed')
SimpleQueue Objects对象¶
SimpleQueue
objects provide the public methods described below.对象提供了下面描述的公共方法。
-
SimpleQueue.
qsize
()¶ Return the approximate size of the queue. Note, qsize() > 0 doesn’t guarantee that a subsequent get() will not block.返回队列的大致大小。注意,qsize()
>0不能保证后续的get()
不会被阻塞。
-
SimpleQueue.
empty
()¶ Return如果队列为空,则返回True
if the queue is empty,False
otherwise.True
,否则返回False
。If empty() returns如果False
it doesn’t guarantee that a subsequent call to get() will not block.empty()
返回False,则不能保证后续对get()
的调用不会被阻塞。
-
SimpleQueue.
put
(item, block=True, timeout=None)¶ Put item into the queue.将item放入队列。The method never blocks and always succeeds (except for potential low-level errors such as failure to allocate memory).该方法从不阻塞并且总是成功(除了潜在的低级错误,如分配内存失败)。The optional args block and timeout are ignored and only provided for compatibility with可选的参数block和timeout被忽略,并且仅为与Queue.put()
.Queue.put()
兼容而提供。CPython implementation detail:
This method has a C implementation which is reentrant.这个方法有一个可重入的C实现。That is, a也就是说,put()
orget()
call can be interrupted by anotherput()
call in the same thread without deadlocking or corrupting internal state inside the queue.put()
或get()
调用可以被同一线程中的另一个put()
调用中断,而不会死锁或损坏队列内部状态。This makes it appropriate for use in destructors such as这使得它适用于析构函数,如__del__
methods orweakref
callbacks.__del__
方法或weakref
回调。
-
SimpleQueue.
put_nowait
(item)¶ Equivalent to等效于put(item, block=False)
, provided for compatibility withQueue.put_nowait()
.put(item, block=False)
,为与Queue.put_nowait()
兼容而提供。
-
SimpleQueue.
get
(block=True, timeout=None)¶ Remove and return an item from the queue.从队列中删除并返回项目。If optional args block is true and timeout is如果可选参数block为None
(the default), block if necessary until an item is available.true
,timeout为None
(默认值),则根据需要进行块,直到项目可用。If timeout is a positive number, it blocks at most timeout seconds and raises the如果timeout为正数,则它最多阻止timeout秒数,如果在该时间内没有项目可用,则引发Empty
exception if no item was available within that time.Empty
异常。Otherwise (block is false), return an item if one is immediately available, else raise the否则(block为Empty
exception (timeout is ignored in that case).false
),如果项目立即可用,则返回一个项目,否则引发Empty
异常(在这种情况下会忽略timeout)。
-
SimpleQueue.
get_nowait
()¶ Equivalent to相当于get(False)
.get(False)
。
See also
- Class
multiprocessing.Queue
A queue class for use in a multi-processing (rather than multi-threading) context.用于多处理(而非多线程)上下文的队列类。
collections.deque
is an alternative implementation of unbounded queues with fast atomic 是无界队列的替代实现,具有快速原子append()
and popleft()
operations that do not require locking and also support indexing.append()
和popleft()
操作,不需要锁定,也支持索引。