Python的Queue模块

  1 NAME
  2     Queue - A multi-producer, multi-consumer queue.
  3
  4 CLASSES
  5     Queue
  6         LifoQueue
  7         PriorityQueue
  8     exceptions.Exception(exceptions.BaseException)
  9         Empty
 10         Full
 11
 12     class Empty(exceptions.Exception)
 13      |  Exception raised by Queue.get(block=0)/get_nowait().
 14      |
 15      |  Method resolution order:
 16      |      Empty
 17      |      exceptions.Exception
 18      |      exceptions.BaseException
 19      |      __builtin__.object
 20      |
 21      |  Data descriptors defined here:
 22      |
 23      |  __weakref__
 24      |      list of weak references to the object (if defined)
 25      |
 26      |  ----------------------------------------------------------------------
 27      |  Methods inherited from exceptions.Exception:
 28      |
 29      |  __init__(...)
 30      |      x.__init__(...) initializes x; see help(type(x)) for signature
 31      |
 32      |  ----------------------------------------------------------------------
 33      |  Data and other attributes inherited from exceptions.Exception:
 34      |
 35      |  __new__ = <built-in method __new__ of type object>
 36      |      T.__new__(S, ...) -> a new object with type S, a subtype of T
 37      |
 38      |  ----------------------------------------------------------------------
 39      |  Methods inherited from exceptions.BaseException:
 40      |
 41      |  __delattr__(...)
 42      |      x.__delattr__(‘name‘) <==> del x.name
 43      |
 44      |  __getattribute__(...)
 45      |      x.__getattribute__(‘name‘) <==> x.name
 46      |
 47      |  __getitem__(...)
 48      |      x.__getitem__(y) <==> x[y]
 49      |
 50      |  __getslice__(...)
 51      |      x.__getslice__(i, j) <==> x[i:j]
 52      |
 53      |      Use of negative indices is not supported.
 54      |
 55      |  __reduce__(...)
 56      |
 57      |  __repr__(...)
 58      |      x.__repr__() <==> repr(x)
 59      |
 60      |  __setattr__(...)
 61      |      x.__setattr__(‘name‘, value) <==> x.name = value
 62      |
 63      |  __setstate__(...)
 64      |
 65      |  __str__(...)
 66      |      x.__str__() <==> str(x)
 67      |
 68      |  __unicode__(...)
 69      |
 70      |  ----------------------------------------------------------------------
 71      |  Data descriptors inherited from exceptions.BaseException:
 72      |
 73      |  __dict__
 74      |
 75      |  args
 76      |
 77      |  message
 78
 79     class Full(exceptions.Exception)
 80      |  Exception raised by Queue.put(block=0)/put_nowait().
 81      |
 82      |  Method resolution order:
 83      |      Full
 84      |      exceptions.Exception
 85      |      exceptions.BaseException
 86      |      __builtin__.object
 87      |
 88      |  Data descriptors defined here:
 89      |
 90      |  __weakref__
 91      |      list of weak references to the object (if defined)
 92      |
 93      |  ----------------------------------------------------------------------
 94      |  Methods inherited from exceptions.Exception:
 95      |
 96      |  __init__(...)
 97      |      x.__init__(...) initializes x; see help(type(x)) for signature
 98      |
 99      |  ----------------------------------------------------------------------
100      |  Data and other attributes inherited from exceptions.Exception:
101      |
102      |  __new__ = <built-in method __new__ of type object>
103      |      T.__new__(S, ...) -> a new object with type S, a subtype of T
104      |
105      |  ----------------------------------------------------------------------
106      |  Methods inherited from exceptions.BaseException:
107      |
108      |  __delattr__(...)
109      |      x.__delattr__(‘name‘) <==> del x.name
110      |
111      |  __getattribute__(...)
112      |      x.__getattribute__(‘name‘) <==> x.name
113      |
114      |  __getitem__(...)
115      |      x.__getitem__(y) <==> x[y]
116      |
117      |  __getslice__(...)
118      |      x.__getslice__(i, j) <==> x[i:j]
119      |
120      |      Use of negative indices is not supported.
121      |
122      |  __reduce__(...)
123      |
124      |  __repr__(...)
125      |      x.__repr__() <==> repr(x)
126      |
127      |  __setattr__(...)
128      |      x.__setattr__(‘name‘, value) <==> x.name = value
129      |
130      |  __setstate__(...)
131      |
132      |  __str__(...)
133      |      x.__str__() <==> str(x)
134      |
135      |  __unicode__(...)
136      |
137      |  ----------------------------------------------------------------------
138      |  Data descriptors inherited from exceptions.BaseException:
139      |
140      |  __dict__
141      |
142      |  args
143      |
144      |  message
145
146     class LifoQueue(Queue)
147      |  Variant of Queue that retrieves most recently added entries first.
148      |
149      |  Methods inherited from Queue:
150      |
151      |  __init__(self, maxsize=0)
152      |
153      |  empty(self)
154      |      Return True if the queue is empty, False otherwise (not reliable!).
155      |
156      |  full(self)
157      |      Return True if the queue is full, False otherwise (not reliable!).
158      |
159      |  get(self, block=True, timeout=None)
160      |      Remove and return an item from the queue.
161      |
162      |      If optional args ‘block‘ is true and ‘timeout‘ is None (the default),
163      |      block if necessary until an item is available. If ‘timeout‘ is
164      |      a non-negative number, it blocks at most ‘timeout‘ seconds and raises
165      |      the Empty exception if no item was available within that time.
166      |      Otherwise (‘block‘ is false), return an item if one is immediately
167      |      available, else raise the Empty exception (‘timeout‘ is ignored
168      |      in that case).
169      |
170      |  get_nowait(self)
171      |      Remove and return an item from the queue without blocking.
172      |
173      |      Only get an item if one is immediately available. Otherwise
174      |      raise the Empty exception.
175      |
176      |  join(self)
177      |      Blocks until all items in the Queue have been gotten and processed.
178      |
179      |      The count of unfinished tasks goes up whenever an item is added to the
180      |      queue. The count goes down whenever a consumer thread calls task_done()
181      |      to indicate the item was retrieved and all work on it is complete.
182      |
183      |      When the count of unfinished tasks drops to zero, join() unblocks.
184      |
185      |  put(self, item, block=True, timeout=None)
186      |      Put an item into the queue.
187      |
188      |      If optional args ‘block‘ is true and ‘timeout‘ is None (the default),
189      |      block if necessary until a free slot is available. If ‘timeout‘ is
190      |      a non-negative number, it blocks at most ‘timeout‘ seconds and raises
191      |      the Full exception if no free slot was available within that time.
192      |      Otherwise (‘block‘ is false), put an item on the queue if a free slot
193      |      is immediately available, else raise the Full exception (‘timeout‘
194      |      is ignored in that case).
195      |
196      |  put_nowait(self, item)
197      |      Put an item into the queue without blocking.
198      |
199      |      Only enqueue the item if a free slot is immediately available.
200      |      Otherwise raise the Full exception.
201      |
202      |  qsize(self)
203      |      Return the approximate size of the queue (not reliable!).
204      |
205      |  task_done(self)
206      |      Indicate that a formerly enqueued task is complete.
207      |
208      |      Used by Queue consumer threads.  For each get() used to fetch a task,
209      |      a subsequent call to task_done() tells the queue that the processing
210      |      on the task is complete.
211      |
212      |      If a join() is currently blocking, it will resume when all items
213      |      have been processed (meaning that a task_done() call was received
214      |      for every item that had been put() into the queue).
215      |
216      |      Raises a ValueError if called more times than there were items
217      |      placed in the queue.
218
219     class PriorityQueue(Queue)
220      |  Variant of Queue that retrieves open entries in priority order (lowest first).
221      |
222      |  Entries are typically tuples of the form:  (priority number, data).
223      |
224      |  Methods inherited from Queue:
225      |
226      |  __init__(self, maxsize=0)
227      |
228      |  empty(self)
229      |      Return True if the queue is empty, False otherwise (not reliable!).
230      |
231      |  full(self)
232      |      Return True if the queue is full, False otherwise (not reliable!).
233      |
234      |  get(self, block=True, timeout=None)
235      |      Remove and return an item from the queue.
236      |
237      |      If optional args ‘block‘ is true and ‘timeout‘ is None (the default),
238      |      block if necessary until an item is available. If ‘timeout‘ is
239      |      a non-negative number, it blocks at most ‘timeout‘ seconds and raises
240      |      the Empty exception if no item was available within that time.
241      |      Otherwise (‘block‘ is false), return an item if one is immediately
242      |      available, else raise the Empty exception (‘timeout‘ is ignored
243      |      in that case).
244      |
245      |  get_nowait(self)
246      |      Remove and return an item from the queue without blocking.
247      |
248      |      Only get an item if one is immediately available. Otherwise
249      |      raise the Empty exception.
250      |
251      |  join(self)
252      |      Blocks until all items in the Queue have been gotten and processed.
253      |
254      |      The count of unfinished tasks goes up whenever an item is added to the
255      |      queue. The count goes down whenever a consumer thread calls task_done()
256      |      to indicate the item was retrieved and all work on it is complete.
257      |
258      |      When the count of unfinished tasks drops to zero, join() unblocks.
259      |
260      |  put(self, item, block=True, timeout=None)
261      |      Put an item into the queue.
262      |
263      |      If optional args ‘block‘ is true and ‘timeout‘ is None (the default),
264      |      block if necessary until a free slot is available. If ‘timeout‘ is
265      |      a non-negative number, it blocks at most ‘timeout‘ seconds and raises
266      |      the Full exception if no free slot was available within that time.
267      |      Otherwise (‘block‘ is false), put an item on the queue if a free slot
268      |      is immediately available, else raise the Full exception (‘timeout‘
269      |      is ignored in that case).
270      |
271      |  put_nowait(self, item)
272      |      Put an item into the queue without blocking.
273      |
274      |      Only enqueue the item if a free slot is immediately available.
275      |      Otherwise raise the Full exception.
276      |
277      |  qsize(self)
278      |      Return the approximate size of the queue (not reliable!).
279      |
280      |  task_done(self)
281      |      Indicate that a formerly enqueued task is complete.
282      |
283      |      Used by Queue consumer threads.  For each get() used to fetch a task,
284      |      a subsequent call to task_done() tells the queue that the processing
285      |      on the task is complete.
286      |
287      |      If a join() is currently blocking, it will resume when all items
288      |      have been processed (meaning that a task_done() call was received
289      |      for every item that had been put() into the queue).
290      |
291      |      Raises a ValueError if called more times than there were items
292      |      placed in the queue.
293
294     class Queue
295      |  Create a queue object with a given maximum size.
296      |
297      |  If maxsize is <= 0, the queue size is infinite.
298      |
299      |  Methods defined here:
300      |
301      |  __init__(self, maxsize=0)
302      |
303      |  empty(self)
304      |      Return True if the queue is empty, False otherwise (not reliable!).
305      |
306      |  full(self)
307      |      Return True if the queue is full, False otherwise (not reliable!).
308      |
309      |  get(self, block=True, timeout=None)
310      |      Remove and return an item from the queue.
311      |
312      |      If optional args ‘block‘ is true and ‘timeout‘ is None (the default),
313      |      block if necessary until an item is available. If ‘timeout‘ is
314      |      a non-negative number, it blocks at most ‘timeout‘ seconds and raises
315      |      the Empty exception if no item was available within that time.
316      |      Otherwise (‘block‘ is false), return an item if one is immediately
317      |      available, else raise the Empty exception (‘timeout‘ is ignored
318      |      in that case).
319      |
320      |  get_nowait(self)
321      |      Remove and return an item from the queue without blocking.
322      |
323      |      Only get an item if one is immediately available. Otherwise
324      |      raise the Empty exception.
325      |
326      |  join(self)
327      |      Blocks until all items in the Queue have been gotten and processed.
328      |
329      |      The count of unfinished tasks goes up whenever an item is added to the
330      |      queue. The count goes down whenever a consumer thread calls task_done()
331      |      to indicate the item was retrieved and all work on it is complete.
332      |
333      |      When the count of unfinished tasks drops to zero, join() unblocks.
334      |
335      |  put(self, item, block=True, timeout=None)
336      |      Put an item into the queue.
337      |
338      |      If optional args ‘block‘ is true and ‘timeout‘ is None (the default),
339      |      block if necessary until a free slot is available. If ‘timeout‘ is
340      |      a non-negative number, it blocks at most ‘timeout‘ seconds and raises
341      |      the Full exception if no free slot was available within that time.
342      |      Otherwise (‘block‘ is false), put an item on the queue if a free slot
343      |      is immediately available, else raise the Full exception (‘timeout‘
344      |      is ignored in that case).
345      |
346      |  put_nowait(self, item)
347      |      Put an item into the queue without blocking.
348      |
349      |      Only enqueue the item if a free slot is immediately available.
350      |      Otherwise raise the Full exception.
351      |
352      |  qsize(self)
353      |      Return the approximate size of the queue (not reliable!).
354      |
355      |  task_done(self)
356      |      Indicate that a formerly enqueued task is complete.
357      |
358      |      Used by Queue consumer threads.  For each get() used to fetch a task,
359      |      a subsequent call to task_done() tells the queue that the processing
360      |      on the task is complete.
361      |
362      |      If a join() is currently blocking, it will resume when all items
363      |      have been processed (meaning that a task_done() call was received
364      |      for every item that had been put() into the queue).
365      |
366      |      Raises a ValueError if called more times than there were items
367      |      placed in the queue.
368
369 DATA
370     __all__ = [‘Empty‘, ‘Full‘, ‘Queue‘, ‘PriorityQueue‘, ‘LifoQueue‘]
时间: 2025-01-14 08:41:53

Python的Queue模块的相关文章

Python队列queue模块

Python中queue模块常用来处理队列相关问题 队列常用于生产者消费者模型,主要功能为提高效率和程序解耦 1. queue模块的基本使用和相关说明 # -*- coding:utf-8 -*- # Author:Wong Du ''' 队列常用于生产者消费者模型, 主要功能为提高效率和程序解耦 ''' import queue """实例化队列对象不同规则的三种方法""" q1 = queue.Queue(maxsize=2) # 先入先出

Python之queue模块

一.queue——同步的队列类 queue模块实现了多生产者,多消费者的队列.当 要求信息必须在多线程间安全交换,这个模块在 线程编程时非常有用 .Queue模块实现了所有要求的锁机制.  说了半天就是Queue模块主要是多线程,保证线程安全使用的. 这个类实现了三种类型的queue,区别仅仅在于进去和取出的位置.在一个FIFO(First In,First Out)队列中,先加先取.在一个LIFO(Last In First Out)的队列中,最后加的先出来(操作起来跟stack一样).pri

后台程序处理(二) python threading - queue 模块使用

由于协程没办法完成(一)中所说的任务模式 接下来就尝试一下使用线程和队列来实现一下这个功能 在实现之前,我们先明确一个问题--python的线程是伪并发的.同一时间只能有一个线程在运行.具体怎样的运作方式由解释器决定 然后回顾一下上一章遇到的问题--return以后,需要另外一个线程去检测之前的操作是否执行成功 因此程序的流程设计应该是这样的: 1 # 大致流程步骤如下 2 # 1.获取参数(接口被访问时触发) 3 request_data = request.form 4 # 2.根据参数查询

【[email&#160;protected]】queue模块-生产者消费者问题

python通过queue模块来提供线程间的通信机制,从而可以让线程分项数据. 个人感觉queue就是管程的概念 一个生产者消费者问题 1 from random import randint 2 from threading import Thread 3 from queue import Queue 4 from time import sleep 5 6 7 def writeq(queue): 8 print('starting put queue...') 9 queue.put('

Python多线程(3)——Queue模块

Queue模块支持先进先出(FIFO)队列,支持多线程的访问,包括一个主要的类型(Queue)和两个异常类(exception classes). Python 2 中的Queue模块在Python 3中更名为 queue. Queue对象的创建 可以通过实例化Queue类型获得队列对象: q = Queue.Queue(maxsize=0) 创建新的队列,参数 maxsize 的含义是: 如果 maxsize > 0:当 q 中的元素达到 maxsize 个时,队列就满了,此时再有一个线程希望

Python Queue模块

创建一个"队列"对象 import Queuemyqueue = Queue.Queue(maxsize = 10) Queue.Queue类即是一个队列的同步实现.队列长度可为无限或者有限.可通过Queue的构造函数的可选参数maxsize来设定队列长度.如果maxsize小于1就表示队列长度无限. 将一个值放入队列中 myqueue.put(10) 调用队列对象的put()方法在队尾插入一个项目.put()有两个参数,第一个item为必需的,为插入项目的值:第二个block为可选参

python Queue模块使用

Python中,队列是线程间最常用的交换数据的形式.Queue模块是提供队列操作的模块,虽然简单易用,但是不小心的话,还是会出现一些意外. 创建一个"队列"对象import Queueq = Queue.Queue(maxsize = 10)Queue.Queue类即是一个队列的同步实现.队列长度可为无限或者有限.可通过Queue的构造函数的可选参数maxsize来设定队列长度.如果maxsize小于1就表示队列长度无限. 将一个值放入队列中q.put(10)    put(item[

Python:线程、进程与协程(3)——Queue模块及源码分析

Queue模块是提供队列操作的模块,队列是线程间最常用的交换数据的形式.该模块提供了三种队列: Queue.Queue(maxsize):先进先出,maxsize是队列的大小,其值为非正数时为无线循环队列 Queue.LifoQueue(maxsize):后进先出,相当于栈 Queue.PriorityQueue(maxsize):优先级队列. 其中LifoQueue,PriorityQueue是Queue的子类.三者拥有以下共同的方法: qsize():返回近似的队列大小.为什么要加"近似&q

Python 单向队列Queue模块详解

单向队列Queue,先进先出 '''A multi-producer, multi-consumer queue.''' try: import threading except ImportError: import dummy_threading as threading from collections import deque from heapq import heappush, heappop from time import monotonic as time __all__ =