Queue

 

import Queue
q = Queue.Queue(2)
print q.empty()
q.put('eeee')
q.put('bb')
print q.qsize()  #返回值为2
q.get() #get一个,则Queue中会空出来一个位置 print q.qsize()  #返回值为1

 print q.queue #查看当前队列中的内容函数

 q.queue.clear()  #清空当前队列spa

import Queue
q = Queue.Queue(2)
print q.empty()
for i in range(1,4):
    try:
        q.put_nowait(i)    #使用put_nowait()将数据放入Queue,若是队列满则抛出Full error。若是直接使用q.put()则当Queue满时,会产生死锁。取数据则使用print q.get_nowait(),同put_nowait()
    except:
        print ‘q is full’
print q.qsize()
while not q.empty():
    print q.get_nowait()  #取值:先进先出
import Queue
q = Queue.Queue(20)
for i in range(1,8):
    try:
        q.put_nowait(i)    
    except:
        print 'q is full'
q.queue.reverse()   #倒序取值:先进后出
while not q.empty():
    print q.get_nowait()

 

Queue的经常使用方法:
   Queue.qsize() #返回队列的大小 
   Queue.empty() #若是队列为空,返回True,反之False 
   Queue.full()  #若是队列满了,返回True,反之False
   Queue.full 与 maxsize 大小对应 
   Queue.get([block[, timeout]]) #获取队列,timeout等待时间,调用队列对象的get()方法从队头删除并返回一个项目。可选参数为block,默认为True。若是队列为空且block为True,get()就使调用线程暂停,直至有项目可用。若是队列为空且block为False,队列将引起Empty异常。 
   Queue.get_nowait() #至关Queue.get(False)
   Queue.put(item)    #非阻塞写入队列,timeout等待时间,调用队列对象的put()方法在队尾插入一个项目。
   put()有两个参数,第一个item为必需的,为插入项目的值;第二个block为可选参数,默认为1。若是队列当前为空且block为1,put()方法就使调用线程暂停,直到空出一个数据单元。若是block为0,put方法将引起Full异常。
   Queue.put_nowait(item) #至关Queue.put(item, False)
   Queue.task_done()   #在完成一项工做以后,Queue.task_done() 函数向任务已经完成的队列发送一个信号Queue.join() 实际上意味着等到队列为空,再执行别的操做.
相关文章
相关标签/搜索