queueA 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, SimpleQueue, whose specific implementation provides additional guarantees in exchange for the smaller functionality.此外,该模块实现了一种“简单”的FIFO队列类型SimpleQueue,其特定实现提供了额外的保证,以换取较小的功能。

The queue module defines the following classes and exceptions:queue模块定义以下类和异常:

classqueue.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小于或等于零,则队列大小是无限的。

classqueue.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小于或等于零,则队列大小是无限的。

classqueue.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)
classqueue.SimpleQueue

Constructor for an unbounded FIFO queue. 无界FIFO队列的构造函数。Simple queues lack advanced functionality such as task tracking.简单队列缺少高级功能,如任务跟踪。

New in version 3.7.版本3.7中新增。

exceptionqueue.Empty

Exception raised when non-blocking get() (or get_nowait()) is called on a Queue object which is empty.对空的Queue对象调用非阻塞的get()(或get_nowait())时引发异常。

exceptionqueue.Full

Exception raised when non-blocking put() (or put_nowait()) is called on a Queue object which is full.对已满的Queue对象调用非阻塞put()(或put_nowait())时引发异常。

Queue ObjectsQueue对象

Queue objects (Queue, LifoQueue, or PriorityQueue) provide the public methods described below.队列对象(QueueLifoQueuePriorityQueue)提供下面描述的公共方法。

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,否则返回FalseIf 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,否则返回FalseIf 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 None (the default), block if necessary until a free slot is available. 如果可选参数blocktrue并且timeoutNone(默认值),则根据需要进行块,直到有可用插槽为止。If timeout is a positive number, it blocks at most timeout seconds and raises the Full exception if no free slot was available within that time. 如果timeout是一个正数,它最多会阻止timeout秒数,如果在该时间内没有可用插槽,则会引发Full异常。Otherwise (block is false), put an item on the queue if a free slot is immediately available, else raise the Full exception (timeout is ignored in that case).否则(blockfalse),如果空闲插槽立即可用,则将项目放入队列,否则引发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 None (the default), block if necessary until an item is available. 如果可选参数blocktruetimeoutNone(默认值),则根据需要进行块,直到项目可用。If timeout is a positive number, it blocks at most timeout seconds and raises the Empty exception if no item was available within that time. 如果timeout为正数,则它最多阻止timeout秒数,如果在该时间内没有项目可用,则引发Empty异常。Otherwise (block is false), return an item if one is immediately available, else raise the Empty exception (timeout is ignored in that case).否则(blockfalse),如果项目立即可用,则返回一个项目,否则引发Empty异常(在这种情况下会忽略timeout)。

Prior to 3.0 on POSIX systems, and for all versions on Windows, if block is true and timeout is None, this operation goes into an uninterruptible wait on an underlying lock. 在POSIX系统上的3.0之前,对于Windows上的所有版本,如果blocktrue并且timeoutNone,则此操作将进入底层锁上的不间断等待。This means that no exceptions can occur, and in particular a SIGINT will not trigger a KeyboardInterrupt.这意味着不会发生异常,特别是SIGINT不会触发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 to task_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 a task_done() call was received for every item that had been put() 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,否则返回FalseIf 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 Queue.put().可选的参数blocktimeout被忽略,并且仅为与Queue.put()兼容而提供。

CPython implementation detail: This method has a C implementation which is reentrant. 这个方法有一个可重入的C实现。That is, a put() or get() call can be interrupted by another put() 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 or weakref callbacks.这使得它适用于析构函数,如__del__方法或weakref回调。

SimpleQueue.put_nowait(item)

Equivalent to put(item, block=False), provided for compatibility with Queue.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 None (the default), block if necessary until an item is available. 如果可选参数blocktruetimeoutNone(默认值),则根据需要进行块,直到项目可用。If timeout is a positive number, it blocks at most timeout seconds and raises the Empty exception if no item was available within that time. 如果timeout为正数,则它最多阻止timeout秒数,如果在该时间内没有项目可用,则引发Empty异常。Otherwise (block is false), return an item if one is immediately available, else raise the Empty exception (timeout is ignored in that case).否则(blockfalse),如果项目立即可用,则返回一个项目,否则引发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()操作,不需要锁定,也支持索引。