18.5.8. 队列集

    Queues:

    asyncio queue API was designed to be close to classes of the queue module (, PriorityQueue, ), but it has no timeout parameter. The asyncio.wait_for() function can be used to cancel a task after a timeout.

    class (maxsize=0, **, loop=None*)

    A queue, useful for coordinating producer and consumer coroutines.

    If maxsize is less than or equal to zero, the queue size is infinite. If it is an integer greater than 0, then yield from put() will block when the queue reaches maxsize, until an item is removed by .

    Unlike the standard library queue, you can reliably know this Queue’s size with , since your single-threaded asyncio application won’t be interrupted between calling qsize() and doing an operation on the Queue.

    这个类不是线程安全的()。

    在 3.4.4 版更改: New join() and methods.

    • empty()

      如果队列为空返回 True ,否则返回 False

    • full()

      如果有 maxsize 个条目在队列中,则返回 。

      注解

      If the Queue was initialized with maxsize=0 (the default), then is never True.

    • coroutine get()

      This method is a coroutine.

      参见

      The method.

    • get_nowait()

      Remove and return an item from the queue.

      立即返回一个队列中的元素,如果队列内有值,否则引发异常 QueueEmpty

    • coroutine put(item)

      Put an item into the queue. If the queue is full, wait until a free slot is available before adding item.

      This method is a .

      参见

      The full() method.

    • (item)

      不阻塞的放一个元素入队列。

    • qsize()

      Number of items in the queue.

    • task_done()

      表明前面排队的任务已经完成,即get出来的元素相关操作已经完成。

      由队列使用者控制。每个 用于获取一个任务,任务最后调用 task_done() 告诉队列,这个任务已经完成。

      如果 当前正在阻塞,在所有条目都被处理后,将解除阻塞(意味着每个 put() 进队列的条目的 都被收到)。

      如果被调用的次数多于放入队列中的项目数量,将引发 ValueError

      3.4.4 新版功能.

    • maxsize

      队列中可存放的元素数量。

    class asyncio.PriorityQueue

    A subclass of ; retrieves entries in priority order (lowest first).

    Entries are typically tuples of the form: (priority number, data).

    class asyncio.LifoQueue

    A subclass of Queue that retrieves most recently added entries first.

    exception asyncio.QueueEmpty

    Exception raised when the method is called on a Queue object which is empty.

    exception