queue --- 一个同步的队列类

源代码: Lib/queue.py


queue 模块实现了多生产者、多消费者队列。这特别适用于消息必须安全地在多线程间交换的线程编程。模块中的 Queue 类实现了所有所需的锁定语义。

模块实现了三种类型的队列,它们的区别仅仅是条目取回的顺序。在 FIFO 队列中,先添加的任务先取回。在 LIFO 队列中,最近被添加的条目先取回(操作类似一个堆栈)。优先级队列中,条目将保持排序( 使用 heapq 模块 ) 并且最小值的条目第一个返回。

在内部,这三个类型的队列使用锁来临时阻塞竞争线程;然而,它们并未被设计用于线程的重入性处理。

此外,模块实现了一个 "简单的" FIFO 队列类型, SimpleQueue ,这个特殊实现为小功能在交换中提供额外的保障。

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. 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.

class queue.LifoQueue(maxsize=0)

LIFO 队列构造函数。 maxsize 是个整数,用于设置可以放入队列中的项目数的上限。当达到这个大小的时候,插入操作将阻塞至队列中的项目被消费掉。如果 maxsize 小于等于零,队列尺寸为无限大。

class queue.PriorityQueue(maxsize=0)

优先级队列构造函数。 maxsize 是个整数,用于设置可以放入队列中的项目数的上限。当达到这个大小的时候,插入操作将阻塞至队列中的项目被消费掉。如果 maxsize 小于等于零,队列尺寸为无限大。

最小值先被取出( 最小值条目是由 sorted(list(entries))[0] 返回的条目)。条目的典型模式是一个以下形式的元组: (priority_number, data)

如果 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

无界的 FIFO 队列构造函数。简单的队列,缺少任务跟踪等高级功能。

3.7 新版功能.

exception queue.Empty

对空的 Queue 对象,调用非阻塞的 get() (or get_nowait()) 时,引发的异常。

exception queue.Full

对满的 Queue 对象,调用非阻塞的 put() (or put_nowait()) 时,引发的异常。

Queue对象

队列对象 (Queue, LifoQueue, 或者 PriorityQueue) 提供下列描述的公共方法。

Queue.qsize()

返回队列的大致大小。注意,qsize() > 0 不保证后续的 get() 不被阻塞,qsize() < maxsize 也不保证 put() 不被阻塞。

Queue.empty()

如果队列为空,返回 True ,否则返回 False 。如果 empty() 返回 True ,不保证后续调用的 put() 不被阻塞。类似的,如果 empty() 返回 False ,也不保证后续调用的 get() 不被阻塞。

Queue.full()

如果队列是满的返回 True ,否则返回 False 。如果 full() 返回 True 不保证后续调用的 get() 不被阻塞。类似的,如果 full() 返回 False 也不保证后续调用的 put() 不被阻塞。

Queue.put(item, block=True, timeout=None)

item 放入队列。如果可选参数 block 是 true 并且 timeoutNone (默认),则在必要时阻塞至有空闲插槽可用。如果 timeout 是个正数,将最多阻塞 timeout 秒,如果在这段时间没有可用的空闲插槽,将引发 Full 异常。反之 (block 是 false),如果空闲插槽立即可用,则把 item 放入队列,否则引发 Full 异常 ( 在这种情况下,timeout 将被忽略)。

Queue.put_nowait(item)

相当于 put(item, False)

Queue.get(block=True, timeout=None)

从队列中移除并返回一个项目。如果可选参数 block 是 true 并且 timeoutNone (默认值),则在必要时阻塞至项目可得到。如果 timeout 是个正数,将最多阻塞 timeout 秒,如果在这段时间内项目不能得到,将引发 Empty 异常。反之 (block 是 false) , 如果一个项目立即可得到,则返回一个项目,否则引发 Empty 异常 (这种情况下,timeout 将被忽略)。

POSIX系统3.0之前,以及所有版本的Windows系统中,如果 block 是 true 并且 timeoutNone , 这个操作将进入基础锁的不间断等待。这意味着,没有异常能发生,尤其是 SIGINT 将不会触发 KeyboardInterrupt 异常。

Queue.get_nowait()

相当于 get(False)

提供了两个方法,用于支持跟踪 排队的任务 是否 被守护的消费者线程 完整的处理。

Queue.task_done()

表示前面排队的任务已经被完成。被队列的消费者线程使用。每个 get() 被用于获取一个任务, 后续调用 task_done() 告诉队列,该任务的处理已经完成。

如果 join() 当前正在阻塞,在.\" Automatically generated by Pandoc 1.19.2 .\" .TH "TQDM" "1" "2015\-2021" "tqdm User Manuals" "" .hy .SH NAME .PP tqdm \- fast, extensible progress bar for Python and CLI .SH SYNOPSIS .PP tqdm [\f[I]options\f[]] .SH DESCRIPTION .PP See . Can be used as a pipe: .IP .nf \f[C] $\ #\ count\ lines\ of\ code $\ cat\ *.py\ |\ tqdm\ |\ wc\ \-l 327it\ [00:00,\ 981773.38it/s] 327 $\ #\ find\ all\ files $\ find\ .\ \-name\ "*.py"\ |\ tqdm\ |\ wc\ \-l 432it\ [00:00,\ 833842.30it/s] 432 #\ ...\ and\ more\ info $\ find\ .\ \-name\ \[aq]*.py\[aq]\ \-exec\ wc\ \-l\ \\{}\ \\;\ \\ \ \ |\ tqdm\ \-\-total\ 432\ \-\-unit\ files\ \-\-desc\ counting\ \\ \ \ |\ awk\ \[aq]{\ sum\ +=\ $1\ };\ END\ {\ print\ sum\ }\[aq] counting:\ 100%|█████████|\ 432/432\ [00:00<00:00,\ 794361.83files/s] 131998 \f[] .fi .SH OPTIONS .TP .B \-h, \-\-help Print this help and exit. .RS .RE .TP .B \-v, \-\-version Print version and exit. .RS .RE .TP .B \-\-desc=\f[I]desc\f[] str, optional. Prefix for the progressbar. .RS .RE .TP .B \-\-total=\f[I]total\f[] int or float, optional. The number of expected iterations. If unspecified, len(iterable) is used if possible. If float("inf") or as a last resort, only basic progress statistics are displayed (no ETA, no progressbar). If \f[C]gui\f[] is True and this parameter needs subsequent updating, specify an initial arbitrary large positive number, e.g. 9e9. .RS .RE .TP .B \-\-leave bool, optional. If [default: True], keeps all traces of the progressbar upon termination of iteration. If \f[C]None\f[], will leave only if \f[C]position\f[] is \f[C]0\f[]. .RS .RE .TP .B \-\-ncols=\f[I]ncols\f[] int, optional. The width of the entire output message. If specified, dynamically resizes the progressbar to stay within this bound. If unspecified, attempts to use environment width. The fallback is a meter width of 10 and no limit for the counter and statistics. If 0, will not print any meter (only stats). .RS .RE .TP .B \-\-mininterval=\f[I]mininterval\f[] float, optional. Minimum progress display update interval [default: 0.1] seconds. .RS .RE .TP .B \-\-maxinterval=\f[I]maxinterval\f[] float, optional. Maximum progress display update interval [default: 10] seconds. Automatically adjusts \f[C]miniters\f[] to correspond to \f[C]mininterval\f[] after long display update lag. Only works if \f[C]dynamic_miniters\f[] or monitor thread is enabled. .RS .RE .TP .B \-\-miniters=\f[I]miniters\f[] int or float, optional. Minimum progress display update interval, in iterations. If 0 and \f[C]dynamic_miniters\f[], will automatically adjust to equal \f[C]mininterval\f[] (more CPU efficient, good for tight loops). If > 0, will skip display of specified number of iterations. Tweak this and \f[C]mininterval\f[] to get very efficient loops. If your progress is erratic with both fast and slow iterations (network, skipping items, etc) you should set miniters=1. .RS .RE .TP .B \-\-ascii=\f[I]ascii\f[] bool or str, optional. If unspecified or False, use unicode (smooth blocks) to fill the meter. The fallback is to use ASCII characters " 123456789#". .RS .RE .TP .B \-\-disable bool, optional. Whether to disable the entire progressbar wrapper [default: False]. If set to None, disable on non\-TTY. .RS .RE .TP .B \-\-unit=\f[I]unit\f[] str, optional. String that will be used to define the unit of each iteration [default: it]. .RS .RE .TP .B \-\-unit\-scale=\f[I]unit_scale\f[] bool or int or float, optional. If 1 or True, the number of iterations will be reduced/scaled automatically and a metric prefix following the International System of Units standard will be added (kilo, mega, etc.) [default: False]. If any other non\-zero number, will scale \f[C]total\f[] and \f[C]n\f[]. .RS .RE .TP .B \-\-dynamic\-ncols bool, optional. If set, constantly alters \f[C]ncols\f[] and \f[C]nrows\f[] to the environment (allowing for window resizes) [default: False]. .RS .RE .TP .B \-\-smoothing=\f[I]smoothing\f[] float, optional. Exponential moving average smoothing factor for speed estimates (ignored in GUI mode). Ranges from 0 (average speed) to 1 (current/instantaneous speed) [default: 0.3]. .RS .RE .TP .B \-\-bar\-format=\f[I]bar_format\f[] str, optional. Specify a custom bar string formatting. May impact performance. [default: \[aq]{l_bar}{bar}{r_bar}\[aq]], where l_bar=\[aq]{desc}: {percentage:3.0f}%|\[aq] and r_bar=\[aq]| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, \[aq] \[aq]{rate_fmt}{postfix}]\[aq] Possible vars: l_bar, bar, r_bar, n, n_fmt, total, total_fmt, percentage, elapsed, elapsed_s, ncols, nrows, desc, unit, rate, rate_fmt, rate_noinv, rate_noinv_fmt, rate_inv, rate_inv_fmt, postfix, unit_divisor, remaining, remaining_s, eta. Note that a trailing ": " is automatically removed after {desc} if the latter is empty. .RS .RE .TP .B \-\-initial=\f[I]initial\f[] int or float, optional. The initial counter value. Useful when restarting a progress bar [default: 0]. If using float, consider specifying \f[C]{n:.3f}\f[] or similar in \f[C]bar_format\f[], or specifying \f[C]unit_scale\f[]. .RS .RE .TP .B \-\-position=\f[I]position\f[] int, optional. Specify the line offset to print this bar (starting from 0) Automatic if unspecified. Useful to manage multiple bars at once (eg, from threads). .RS .RE .TP .B \-\-postfix=\f[I]postfix\f[] dict or *, optional. Specify additional stats to display at the end of the bar. Calls \f[C]set_postfix(**postfix)\f[] if possible (dict). .RS .RE .TP .B \-\-unit\-divisor=\f[I]unit_divisor\f[] float, optional. [default: 1000], ignored unless \f[C]unit_scale\f[] is True. .RS .RE .TP .B \-\-write\-bytes bool, optional. If (default: None) and \f[C]file\f[] is unspecified, bytes will be written in Python 2. If \f[C]True\f[] will also write bytes. In all other cases will default to unicode. .RS .RE .TP .B \-\-lock\-args=\f[I]lock_args\f[] tuple, optional. Passed to \f[C]refresh\f[] for intermediate output (initialisation, iterating, and updating). .RS .RE .TP .B \-\-nrows=\f[I]nrows\f[] int, optional. The screen height. If specified, hides nested bars outside this bound. If unspecified, attempts to use environment height. The fallback is 20. .RS .RE .TP .B \-\-colour=\f[I]colour\f[] str, optional. Bar colour (e.g. \[aq]green\[aq], \[aq]#00ff00\[aq]). .RS .RE .TP .B \-\-delay=\f[I]delay\f[] float, optional. Don\[aq]t display until [default: 0] seconds have elapsed. .RS .RE .TP .B \-\-delim=\f[I]delim\f[] chr, optional. Delimiting character [default: \[aq]\\n\[aq]]. Use \[aq]\\0\[aq] for null. N.B.: on Windows systems, Python converts \[aq]\\n\[aq] to \[aq]\\r\\n\[aq]. .RS .RE .TP .B \-\-buf\-size=\f[I]buf_size\f[] int, optional. String buffer size in bytes [default: 256] used when \f[C]delim\f[] is specified. .RS .RE .TP .B \-\-bytes bool, optional. If true, will count bytes, ignore \f[C]delim\f[], and default \f[C]unit_scale\f[] to True, \f[C]unit_divisor\f[] to 1024, and \f[C]unit\f[] to \[aq]B\[aq]. .RS .RE .TP .B \-\-tee bool, optional. If true, passes \f[C]stdin\f[] to both \f[C]stderr\f[] and \f[C]stdout\f[]. .RS .RE .TP .B \-\-update bool, optional. If true, will treat input as newly elapsed iterations, i.e. numbers to pass to \f[C]update()\f[]. Note that this is slow (~2e5 it/s) since every input must be decoded as a number. .RS .RE .TP .B \-\-update\-to bool, optional. If true, will treat input as total elapsed iterations, i.e. numbers to assign to \f[C]self.n\f[]. Note that this is slow (~2e5 it/s) since every input must be decoded as a number. .RS .RE .TP .B \-\-null bool, optional. If true, will discard input (no stdout). .RS .RE .TP .B \-\-manpath=\f[I]manpath\f[] str, optional. Directory in which to install tqdm man pages. .RS .RE .TP .B \-\-comppath=\f[I]comppath\f[] str, optional. Directory in which to place tqdm completion. .RS .RE .TP .B \-\-log=\f[I]log\f[] str, optional. CRITICAL|FATAL|ERROR|WARN(ING)|[default: \[aq]INFO\[aq]]|DEBUG|NOTSET. .RS .RE .SH AUTHORS tqdm developers . INDX( qn( sp\FG%hRfFGSgbWbWbWb6 activatetalkFGpZfFG$obobobob  activate.cshPlFGp\fFGpbqb>qbqb activate.fishjFGpZfFG(kb(kb#kb(kb0" Activate.ps1PlFGpZfFGpbqb>qbqb ACTIVA~1.FISPIGhVfFG?b?b?b?b chardetectlIGhRfFG?b?b?b?b CHARDE~1talIGpZfFGQ bQ bTbQ b  easy_installPIGxbfFGbb*bb  eay_install-3.8NIGhRfFGQ bQ bTbQ b  EASY_I~1GIGhVfFGbb*bb  EASY_I~1.8MGXHfFG7b7b^b7b pipMG`JfFGJobJobHbJob pip3onMG`NfFGbbbb pi3.8hFG`NfFGPbPbbPb pythoniFG`PfFG b b b b python3GG`JfFGTkbTbT-bTb tqdmh4>上一个主题

sched --- 事件调度器

下一个主题

_thread --- 底层多线程 API