python Queue模块

来源:互联网 发布:java面向切面编程 编辑:程序博客网 时间:2024/05/17 23:48

BY:http://blog.csdn.net/yatere/article/details/6668006

 

 

创建一个“队列”对象


import Queue
myqueue = Queue.Queue(maxsize = 10)


Queue.Queue类即是一个队列的同步实现。队列长度可为无限或者有限。可通过Queue的构造函数的可选参数maxsize来设定队列长度。如果maxsize小于1就表示队列长度无限。


将一个值放入队列中


myqueue.put(10)


调用队列对象的put()方法在队尾插入一个项目。put()有两个参数,第一个item为必需的,为插入项目的值;第二个block为可选参数,默认为1。如果队列当前为空且block为1,put()方法就使调用线程暂停,直到空出一个数据单元。如果block为0,put方法将引发Full异常。


将一个值从队列中取出


myqueue.get()


调用队列对象的get()方法从队头删除并返回一个项目。可选参数为block,默认为True。如果队列为空且block为True,get()就使调用线程暂停,直至有项目可用。如果队列为空且block为False,队列将引发Empty异常。




python queue模块有三种队列:
1、python queue模块的FIFO队列先进先出。
2、LIFO类似于堆。即先进后出。
3、还有一种是优先级队列级别越低越先出来。


针对这三种队列分别有三个构造函数:
1、class Queue.Queue(maxsize) FIFO
2、class Queue.LifoQueue(maxsize) LIFO
3、class 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.task_done() 函数向任务已经完成的队列发送一个信号

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


稍后补上自己的代码例子


[python] view plaincopyprint?
  1. #!/usr/bin/env python 
  2. import Queue  
  3. import threading  
  4. import time  
  5. import random   
  6.   
  7. q=Queue.Queue(0)  
  8. NUM_WORKERS = 3   
  9.   
  10. class MyThread(threading.Thread): 
  11.     """A worker thread."""  
  12.     def __init__(self, input, worktype): 
  13.         self._jobq = input  
  14.         self._work_type = worktype  
  15.         threading.Thread.__init__(self)  
  16.     def run(self): 
  17.         """
  18.         Get a job and process it.
  19.         Stop when there's no more jobs
  20.         """  
  21.         while True
  22.             if self._jobq.qsize()>0
  23.                 job = self._jobq.get()  
  24.                 worktype=self._work_type  
  25.                 self._process_job(job,worktype)  
  26.             else
  27.                 break  
  28.     def _process_job(self, job,worktype): 
  29.         """
  30.         Do useful work here.
  31.         worktype: let this thread do different work
  32.         1,do list
  33.         2,do item
  34.         3,,,
  35.         """  
  36.         doJob(job)   
  37.   
  38. def doJob(job): 
  39.     """
  40.     do work function 1
  41.     """  
  42.     time.sleep(random.random()*3)  
  43.     print "doing ",job  
  44. if __name__=='__main__':   
  45.   
  46.     print "begin..."  
  47.     #put some work to q 
  48.     for i in range(NUM_WORKERS*2): 
  49.         q.put(i)  
  50.     #print total job q's size 
  51.     print "job q'size",q.qsize()  
  52.     #start threads to work 
  53.     for x in range(NUM_WORKERS): 
  54.         MyThread(q,x).start()  
  55.     #if q is not empty, wait 
  56.     #while q.qsize()>0: 
  57.     #    time.sleep(0.1)