python Queue

文档:http://docs.python.org/2/library/queue.html

Source code: Lib/Queue.py


The Queue module implements multi-producer, multi-consumer queues. 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. It depends on the availability of thread support in Python; see the threading module.

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. In a LIFO queue, the most recently added entry is the first retrieved (operating like a stack). With a priority queue, the entries are kept sorted (using the heapq module) and the lowest valued entry is retrieved first.

The Queue module defines the following classes and exceptions:

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)

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

New in version 2.6.

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

The lowest valued entries are retrieved first (the lowest valued entry is the one returned by sorted(list(entries))[0]). A typical pattern for entries is a tuple in the form: (priority_number, data).

New in version 2.6.

exception Queue.Empty

Exception raised when non-blocking get() (or get_nowait()) is called on a Queue object which is empty.

exception Queue.Full

Exception raised when non-blocking put() (or put_nowait()) is called on a Queue object which is full.

See also

 

collections.deque is an alternative implementation of unbounded queues with fast atomic append() and popleft() operations that do not require locking.

 

此模块一般用于和多线程配合

先进先出  q = Queue.Queue(maxsize)

后进先出  a = Queue.LifoQueue(maxsize)

优先级  Queue.PriorityQueue(maxsize)

Queue.qsize() 返回队列的大小

Queue.empty() 如果队列为空,返回True,反之False

Queue.full() 如果队列满了,返回True,反之False

Queue.full 与 maxsize 大小对应

Queue.get([block[, timeout]]) 获取队列,timeout等待时间

Queue.get_nowait() 相当Queue.get(False)

非阻塞 Queue.put(item) 写入队列,timeout等待时间

Queue.put_nowait(item) 相当Queue.put(item, False)

Queue.task_done() 在完成一项工作之后,函数向任务已经完成的队列发送一个信号

Queue.join(): 实际上意味着等到队列为空,再执行别的操作 。

这个不是很理解,官方文档:

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 callstask_done() to indicate that the item was retrieved and all work on it is complete. When the count of unfinished tasks drops to zero, join()unblocks.

python Queue
#coding=utf-8
import Queue
q=Queue.Queue(5)
print q.empty()
for i in xrange(5):
    q.put(i)

while not q.empty():
    print q.get(),
    
python Queue

 

注意,不能用while !q.empty(): 这样的语法。

 

 

python Queue

上一篇:python读写xml


下一篇:转发 python中file和open有什么区别