python的队列queue

Help on module queue:

NAME
    queue - A multi-producer, multi-consumer queue.队列——一个多生产者、多消费者的队列。

CLASSES
    builtins.Exception(builtins.BaseException)
        _queue.Empty
        Full
    builtins.object
        _queue.SimpleQueue
            Queue    FIFO即First in First Out,先进先出。
            LifoQueue    LIFO即Last in First Out,后进先出。
            PriorityQueue    构造一个优先队列。

    
    class Empty(builtins.Exception)
     |  Exception raised by Queue.get(block=0)/get_nowait().
     |  
     |  Method resolution order:
     |      Empty
     |      builtins.Exception
     |      builtins.BaseException
     |      builtins.object
     |  
     |  Data descriptors defined here:
     |  
     |  __weakref__
     |      list of weak references to the object (if defined)
     |  
     |  ----------------------------------------------------------------------
     |  Methods inherited from builtins.Exception:
     |  
     |  __init__(self, /, *args, **kwargs)
     |      Initialize self.  See help(type(self)) for accurate signature.
     |  
     |  ----------------------------------------------------------------------
     |  Static methods inherited from builtins.Exception:
     |  
     |  __new__(*args, **kwargs) from builtins.type
     |      Create and return a new object.  See help(type) for accurate signature.
     |  
     |  ----------------------------------------------------------------------
     |  Methods inherited from builtins.BaseException:
     |  
     |  __delattr__(self, name, /)
     |      Implement delattr(self, name).
     |  
     |  __getattribute__(self, name, /)
     |      Return getattr(self, name).
     |  
     |  __reduce__(...)
     |      Helper for pickle.
     |  
     |  __repr__(self, /)
     |      Return repr(self).
     |  
     |  __setattr__(self, name, value, /)
     |      Implement setattr(self, name, value).
     |  
     |  __setstate__(...)
     |  
     |  __str__(self, /)
     |      Return str(self).
     |  
     |  with_traceback(...)
     |      Exception.with_traceback(tb) --
     |      set self.__traceback__ to tb and return self.
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors inherited from builtins.BaseException:
     |  
     |  __cause__
     |      exception cause
     |  
     |  __context__
     |      exception context
     |  
     |  __dict__
     |  
     |  __suppress_context__
     |  
     |  __traceback__
     |  
     |  args
    
    class Full(builtins.Exception)
     |  Exception raised by Queue.put(block=0)/put_nowait().
     |  
     |  Method resolution order:
     |      Full
     |      builtins.Exception
     |      builtins.BaseException
     |      builtins.object
     |  
     |  Data descriptors defined here:
     |  
     |  __weakref__
     |      list of weak references to the object (if defined)
     |  
     |  ----------------------------------------------------------------------
     |  Methods inherited from builtins.Exception:
     |  
     |  __init__(self, /, *args, **kwargs)
     |      Initialize self.  See help(type(self)) for accurate signature.
     |  
     |  ----------------------------------------------------------------------
     |  Static methods inherited from builtins.Exception:
     |  
     |  __new__(*args, **kwargs) from builtins.type
     |      Create and return a new object.  See help(type) for accurate signature.
     |  
     |  ----------------------------------------------------------------------
     |  Methods inherited from builtins.BaseException:
     |  
     |  __delattr__(self, name, /)
     |      Implement delattr(self, name).
     |  
     |  __getattribute__(self, name, /)
     |      Return getattr(self, name).
     |  
     |  __reduce__(...)
     |      Helper for pickle.
     |  
     |  __repr__(self, /)
     |      Return repr(self).
     |  
     |  __setattr__(self, name, value, /)
     |      Implement setattr(self, name, value).
     |  
     |  __setstate__(...)
     |  
     |  __str__(self, /)
     |      Return str(self).
     |  
     |  with_traceback(...)
     |      Exception.with_traceback(tb) --
     |      set self.__traceback__ to tb and return self.
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors inherited from builtins.BaseException:
     |  
     |  __cause__
     |      exception cause
     |  
     |  __context__
     |      exception context
     |  
     |  __dict__
     |  
     |  __suppress_context__
     |  
     |  __traceback__
     |  
     |  args
    
    一、class LifoQueue(Queue)
     |  LifoQueue(maxsize=0)

     |  
     |  Variant of Queue that retrieves most recently added entries first.  LIFO即Last in First Out,后进先出。与栈的类似,使用也很简单,maxsize是个整数,指明了队列中能存放的数据个数的上限。一旦达到上限,插入会导致阻塞,直到队列中的数据被消费掉。如果maxsize小于或者等于0,队列大小没有限制。

>>> from queue import LifoQueue
>>> 
>>> lq = LifoQueue()
>>> for i in range(1, 6):
	lq.put(i)

	
>>> type(lq)

>>> lq

>>> 
>>> while not lq.empty():
	print(lq.get())

	
5
4
3
2
1
>>> 


     |  
     |  Method resolution order:
     |      LifoQueue
     |      Queue
     |      builtins.object
     |  
     |  Methods inherited from Queue:
     |  
     |  __init__(self, maxsize=0)
     |      Initialize self.  See help(type(self)) for accurate signature.
     |  
     |  empty(self)
     |      Return True if the queue is empty, False otherwise (not reliable!).如果队列为空,返回True,反之False。

     |      
     |      This method is likely to be removed at some point.  Use qsize() == 0
     |      as a direct substitute, but be aware that either approach risks a race
     |      condition where a queue can grow before the result of empty() or
     |      qsize() can be used.
     |      
     |      To create code that needs to wait for all queued tasks to be
     |      completed, the preferred technique is to use the join() method.
     |  
     |  full(self)
     |      Return True if the queue is full, False otherwise (not reliable!).如果队列满了,返回True,反之False。

     |      
     |      This method is likely to be removed at some point.  Use qsize() >= n
     |      as a direct substitute, but be aware that either approach risks a race
     |      condition where a queue can shrink before the result of full() or
     |      qsize() can be used.
     |  
     |  get(self, block=True, timeout=None)
     |      Remove and return an item from the queue.读队列,timeout等待时间。
     |      

     |      If optional args 'block' is true and 'timeout' is None (the default),
     |      block if necessary until an item is available. If 'timeout' is
     |      a non-negative number, it blocks at most 'timeout' seconds and raises
     |      the Empty exception if no item was available within that time.
     |      Otherwise ('block' is false), return an item if one is immediately
     |      available, else raise the Empty exception ('timeout' is ignored
     |      in that case).

        如果可选的args 'block'为true,而'timeout'为None(缺省值),则在项目可用之前,如果有必要,可以使用block。如果“timeout”是一个非负数,那么它最多会阻塞“timeout”秒,如果在这段时间内没有可用的项,则会引发空异常。否则('block'为false),返回一个立即可用的项,否则引发空异常(在这种情况下忽略'timeout')。
     |  
     |  get_nowait(self)
     |      Remove and return an item from the queue without blocking.
     |      
     |      Only get an item if one is immediately available. Otherwise
     |      raise the Empty exception.
     |  
     |  join(self)
     |      Blocks until all items in the Queue have been gotten and processed.

        阻塞调用线程,直到队列中的所有任务被处理掉。只要有数据被加入队列,未完成的任务数就会增加。当消费者线程调用task_done()(意味着有消费者取得任务并完成任务),未完成的任务数就会减少。当未完成的任务数降到0,join()解除阻塞。
     |      
     |      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 the item was retrieved and all work on it is complete.
     |      
     |      When the count of unfinished tasks drops to zero, join() unblocks.
     |  
     |  put(self, item, block=True, timeout=None)
     |      Put an item into the queue.写队列,timeout等待时间。

     |      
     |      If optional args 'block' is true and 'timeout' is None (the default),
     |      block if necessary until a free slot is available. If 'timeout' is
     |      a non-negative number, it blocks at most 'timeout' seconds and raises
     |      the Full exception if no free slot was available within that time.
     |      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).

        如果可选的args 'block'为true,而'timeout'为None(缺省值),则在空闲插槽可用之前,如果有必要,可以使用block。如果“timeout”是非负数,那么它最多会阻塞“timeout”秒,如果在这段时间内没有可用的空闲插槽,则会引发完整的异常。否则('block'为false),如果一个空闲插槽立即可用,则将一个项放到队列中,否则引发完全异常(在这种情况下忽略'timeout')。
     |  
     |  put_nowait(self, item)
     |      Put an item into the queue without blocking.
     |      
     |      Only enqueue the item if a free slot is immediately available.
     |      Otherwise raise the Full exception.
     |  
     |  qsize(self)
     |      Return the approximate size of the queue (not reliable!).返回队列的大小。

     |  
     |  task_done(self)
     |      Indicate that a formerly enqueued task is complete.

        意味着之前入队的一个任务已经完成。由队列的消费者线程调用。每一个get()调用得到一个任务,接下来的task_done()调用告诉队列该任务已经处理完毕。如果当前一个join()正在阻塞,它将在队列中的所有任务都处理完时恢复执行(即每一个由put()调用入队的任务都有一个对应的task_done()调用)。
     |      
     |      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.
     |      
     |      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).
     |      
     |      Raises a ValueError if called more times than there were items
     |      placed in the queue.
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors inherited from Queue:
     |  
     |  __dict__
     |      dictionary for instance variables (if defined)
     |  
     |  __weakref__
     |      list of weak references to the object (if defined)
    
    二、class PriorityQueue(Queue)
     |  PriorityQueue(maxsize=0)

     |  
     |  Variant of Queue that retrieves open entries in priority order (lowest first).队列的变体,按优先级顺序(最低优先级)打开条目。
     |  
     |  Entries are typically tuples of the form:  (priority number, data).条目通常是表单的元组:(优先级号、数据)。
     |  

>>> from queue import PriorityQueue
>>> 
>>> lst = [(2, 'two'), (4, 'four'), (6, 'six'), (8, 'eight'), (1, 'one'), (3, 'three'), (5, 'five'), (7, 'seven'), (9, 'nine')]
>>> [(2, 'two'), (4, 'four'), (6, 'six'), (8, 'eight'), (1, 'one'), (3, 'three'), (5, 'five'), (7, 'seven'), (9, 'nine')]
[(2, 'two'), (4, 'four'), (6, 'six'), (8, 'eight'), (1, 'one'), (3, 'three'), (5, 'five'), (7, 'seven'), (9, 'nine')]
>>> 
>>> pq = PriorityQueue()
>>> for i, v in enumerate(lst):
	pq.put(v)

	
>>> type(pq)

>>> pq

>>> 
>>> while not pq.empty():
	print(pq.get())

	
(1, 'one')
(2, 'two')
(3, 'three')
(4, 'four')
(5, 'five')
(6, 'six')
(7, 'seven')
(8, 'eight')
(9, 'nine')
>>> 


     |  Method resolution order:
     |      PriorityQueue
     |      Queue
     |      builtins.object
     |  
     |  Methods inherited from Queue:
     |  
     |  __init__(self, maxsize=0)
     |      Initialize self.  See help(type(self)) for accurate signature.
     |  
     |  empty(self)
     |      Return True if the queue is empty, False otherwise (not reliable!).
     |      
     |      This method is likely to be removed at some point.  Use qsize() == 0
     |      as a direct substitute, but be aware that either approach risks a race
     |      condition where a queue can grow before the result of empty() or
     |      qsize() can be used.
     |      
     |      To create code that needs to wait for all queued tasks to be
     |      completed, the preferred technique is to use the join() method.
     |  
     |  full(self)
     |      Return True if the queue is full, False otherwise (not reliable!).
     |      
     |      This method is likely to be removed at some point.  Use qsize() >= n
     |      as a direct substitute, but be aware that either approach risks a race
     |      condition where a queue can shrink before the result of full() or
     |      qsize() can be used.
     |  
     |  get(self, 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. If 'timeout' is
     |      a non-negative number, it blocks at most 'timeout' seconds and raises
     |      the Empty exception if no item was available within that time.
     |      Otherwise ('block' is false), return an item if one is immediately
     |      available, else raise the Empty exception ('timeout' is ignored
     |      in that case).
     |  
     |  get_nowait(self)
     |      Remove and return an item from the queue without blocking.
     |      
     |      Only get an item if one is immediately available. Otherwise
     |      raise the Empty exception.
     |  
     |  join(self)
     |      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 the item was retrieved and all work on it is complete.
     |      
     |      When the count of unfinished tasks drops to zero, join() unblocks.
     |  
     |  put(self, item, block=True, timeout=None)
     |      Put an item into the queue.
     |      
     |      If optional args 'block' is true and 'timeout' is None (the default),
     |      block if necessary until a free slot is available. If 'timeout' is
     |      a non-negative number, it blocks at most 'timeout' seconds and raises
     |      the Full exception if no free slot was available within that time.
     |      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).
     |  
     |  put_nowait(self, item)
     |      Put an item into the queue without blocking.
     |      
     |      Only enqueue the item if a free slot is immediately available.
     |      Otherwise raise the Full exception.
     |  
     |  qsize(self)
     |      Return the approximate size of the queue (not reliable!).
     |  
     |  task_done(self)
     |      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.
     |      
     |      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).
     |      
     |      Raises a ValueError if called more times than there were items
     |      placed in the queue.
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors inherited from Queue:
     |  
     |  __dict__
     |      dictionary for instance variables (if defined)
     |  
     |  __weakref__
     |      list of weak references to the object (if defined)
    
    三、class Queue(builtins.object)
     |  Queue(maxsize=0)
     |  
     |  Create a queue object with a given maximum size.  FIFO, 如果maxsize小于1就表示队列长度无限
     |  
     |  If maxsize is <= 0, the queue size is infinite.

>>> from queue import Queue
>>> 
>>> q = Queue()
>>> for i in range(1, 6):
	q.put(i)

	
>>> type(q)

>>> q

>>> 
>>> while not q.empty():
	print(q.get())

	
1
2
3
4
5
>>> 


     |  
     |  Methods defined here:
     |  
     |  __init__(self, maxsize=0)
     |      Initialize self.  See help(type(self)) for accurate signature.
     |  
     |  empty(self)
     |      Return True if the queue is empty, False otherwise (not reliable!).
     |      
     |      This method is likely to be removed at some point.  Use qsize() == 0
     |      as a direct substitute, but be aware that either approach risks a race
     |      condition where a queue can grow before the result of empty() or
     |      qsize() can be used.
     |      
     |      To create code that needs to wait for all queued tasks to be
     |      completed, the preferred technique is to use the join() method.
     |  
     |  full(self)
     |      Return True if the queue is full, False otherwise (not reliable!).
     |      
     |      This method is likely to be removed at some point.  Use qsize() >= n
     |      as a direct substitute, but be aware that either approach risks a race
     |      condition where a queue can shrink before the result of full() or
     |      qsize() can be used.
     |  
     |  get(self, 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. If 'timeout' is
     |      a non-negative number, it blocks at most 'timeout' seconds and raises
     |      the Empty exception if no item was available within that time.
     |      Otherwise ('block' is false), return an item if one is immediately
     |      available, else raise the Empty exception ('timeout' is ignored
     |      in that case).
     |  
     |  get_nowait(self)
     |      Remove and return an item from the queue without blocking.
     |      
     |      Only get an item if one is immediately available. Otherwise
     |      raise the Empty exception.
     |  
     |  join(self)
     |      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 the item was retrieved and all work on it is complete.
     |      
     |      When the count of unfinished tasks drops to zero, join() unblocks.
     |  
     |  put(self, item, block=True, timeout=None)
     |      Put an item into the queue.
     |      
     |      If optional args 'block' is true and 'timeout' is None (the default),
     |      block if necessary until a free slot is available. If 'timeout' is
     |      a non-negative number, it blocks at most 'timeout' seconds and raises
     |      the Full exception if no free slot was available within that time.
     |      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).
     |  
     |  put_nowait(self, item)
     |      Put an item into the queue without blocking.
     |      
     |      Only enqueue the item if a free slot is immediately available.
     |      Otherwise raise the Full exception.
     |  
     |  qsize(self)
     |      Return the approximate size of the queue (not reliable!).
     |  
     |  task_done(self)
     |      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.
     |      
     |      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).
     |      
     |      Raises a ValueError if called more times than there were items
     |      placed in the queue.
     |  
     |  ----------------------------------------------------------------------
     |  Data descriptors defined here:
     |  
     |  __dict__
     |      dictionary for instance variables (if defined)
     |  
     |  __weakref__
     |      list of weak references to the object (if defined)
    
    class SimpleQueue(builtins.object)
     |  Simple, unbounded, reentrant FIFO queue.
     |  
     |  Methods defined here:
     |  
     |  empty(self, /)
     |      Return True if the queue is empty, False otherwise (not reliable!).
     |  
     |  get(self, /, 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. If 'timeout' is
     |      a non-negative number, it blocks at most 'timeout' seconds and raises
     |      the Empty exception if no item was available within that time.
     |      Otherwise ('block' is false), return an item if one is immediately
     |      available, else raise the Empty exception ('timeout' is ignored
     |      in that case).
     |  
     |  get_nowait(self, /)
     |      Remove and return an item from the queue without blocking.
     |      
     |      Only get an item if one is immediately available. Otherwise
     |      raise the Empty exception.
     |  
     |  put(self, /, item, block=True, timeout=None)
     |      Put the item on the queue.
     |      
     |      The optional 'block' and 'timeout' arguments are ignored, as this method
     |      never blocks.  They are provided for compatibility with the Queue class.
     |  
     |  put_nowait(self, /, item)
     |      Put an item into the queue without blocking.
     |      
     |      This is exactly equivalent to `put(item)` and is only provided
     |      for compatibility with the Queue class.
     |  
     |  qsize(self, /)
     |      Return the approximate size of the queue (not reliable!).
     |  
     |  ----------------------------------------------------------------------
     |  Static methods defined here:
     |  
     |  __new__(*args, **kwargs) from builtins.type
     |      Create and return a new object.  See help(type) for accurate signature.

DATA
    __all__ = ['Empty', 'Full', 'Queue', 'PriorityQueue', 'LifoQueue', 'Si...

FILE
    c:\python37\lib\queue.py


>>> 

 

你可能感兴趣的:(Python)