分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow
也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!
本文转载至:
作为标准库中的queue模块,在实际的开发中也是很常见的。其实数据结构这种东西,在任何的编程语言中都是相通的,无非表达方式有些不同罢了。那么, 今天就来看看如何在Python中使用队列。
创建一个队列
import Queueq = Queue.Queue(maxsize = 10)
- 1
- 2
可见对于模块Queue里面的类Queue而言,是可以在初始化的时候指定队列大小的。队列长度可为无限或者有限。由可选参数maxsize来设定队列长度。需要注意的是:
如果maxsize小于1就表示队列长度无限。但这样的话,意义不大。
入队
q.put(item)
- 1
调用队列对象的put()方法在队尾插入一个项目。put()有两个参数:
- 第一个item为必需的,为插入项目的值;
- 第二个block为可选参数,默认为1。
如果队列当前为空且block为1,put()方法就使调用线程暂停,直到空出一个数据单元。如果block为0,put方法将引发Full异常。
出队
q.get()
- 1
调用队列对象的get()方法从队头删除并返回一个项目。可选参数为block,默认为True。
如果队列为空且block为True,get()就使调用线程暂停,直至有项目可用。如果队列为空且block为False,队列将引发Empty异常。
队列模式
Python 中Queue模块有三种队列及构造函数:
Python Queue模块的FIFO队列先进先出。
class Queue.Queue(maxsize)
LIFO类似于堆,即先进后出。
class Queue.LifoQueue(maxsize)
还有一种是优先级队列级别越低越先出来。
class Queue.PriorityQueue(maxsize)
方法集合
q = Queue.Queue()):
- 1
q.qsize() 返回队列的大小
q.empty() 如果队列为空,返回True,反之False
q.full() 如果队列满了,返回True,反之False
q.full 与 maxsize 大小对应
q.get([block[, timeout]]) 获取队列,timeout等待时间
q.get_nowait() 相当q.get(False) 非阻塞 q.put(item) 写入队列,timeout等待时间
q.put_nowait(item) 相当q.put(item, False)
q.task_done() 在完成一项工作之后,q.task_done() 函数向任务已经完成的队列发送一个信号
q.join() 实际上意味着等到队列为空,再执行别的操作
实例
下面来看一个典型的生产者与消费者问题。
#!/usr/bin/env python#coding:utf8import random,threading,timefrom Queue import Queue#Producer threadclass Producer(threading.Thread): def __init__(self, t_name, queue): threading.Thread.__init__(self,name=t_name) self.data=queue def run(self): for i in range(10): #随机产生10个数字 ,可以修改为任意大小 randomnum=random.randint(1,99) print "%s: %s is producing %d to the queue!" % (time.ctime(), self.getName(), randomnum) self.data.put(randomnum) #将数据依次存入队列 time.sleep(1) print "%s: %s finished!" %(time.ctime(), self.getName())#Consumer threadclass Consumer_even(threading.Thread): def __init__(self,t_name,queue): threading.Thread.__init__(self,name=t_name) self.data=queue def run(self): while 1: try: val_even = self.data.get(1,5) #get(self, block=True, timeout=None) ,1就是阻塞等待,5是超时5秒 if val_even%2==0: print "%s: %s is consuming. %d in the queue is consumed!" % (time.ctime(),self.getName(),val_even) time.sleep(2) else: self.data.put(val_even) time.sleep(2) except: #等待输入,超过5秒 就报异常 print "%s: %s finished!" %(time.ctime(),self.getName()) breakclass Consumer_odd(threading.Thread): def __init__(self,t_name,queue): threading.Thread.__init__(self, name=t_name) self.data=queue def run(self): while 1: try: val_odd = self.data.get(1,5) if val_odd%2!=0: print "%s: %s is consuming. %d in the queue is consumed!" % (time.ctime(), self.getName(), val_odd) time.sleep(2) else: self.data.put(val_odd) time.sleep(2) except: print "%s: %s finished!" % (time.ctime(), self.getName()) break#Main threaddef main(): queue = Queue() producer = Producer('Pro.', queue) consumer_even = Consumer_even('Con_even.', queue) consumer_odd = Consumer_odd('Con_odd.',queue) producer.start() consumer_even.start() consumer_odd.start() producer.join() consumer_even.join() consumer_odd.join() print 'All threads terminate!'if __name__ == '__main__': main()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68