首先,了解Queue模块
队列模块实现了一个多生产者和多消费者队列. 它特别适用于必须在多个线程之间安全地交换信息的多线程程序. 此模块中的Queue类实现所有必要的锁语义. 这取决于Python中线程支持的可用性. 见.
该模块实现三种类型的队列: FIFO(先进先出,先进先出,默认为队列),LIFO(后进先出,后进先出)和基于优先级的队列. 以下是常用方法:
先进先出 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.put(item) 写入队列,timeout等待时间 非阻塞
Queue.get([block[, timeout]]) 获取队列,timeout等待时间
Queue.get_nowait() 相当Queue.get(False)
Queue.put_nowait(item) 相当Queue.put(item, False)
Queue.task_done() 在完成一项工作之后,函数向任务已经完成的队列发送一个信号
Queue.join(): 实际上意味着等到队列为空,再执行别的操作
有关更多详细信息,请参阅Python标准库的Queue模块的介绍.
第二,队列显示
1,FIFO(先进先出)
import Queue
q = Queue.Queue()
for i in range(5):
q.put(i)
while not q.empty():
print q.get()
输出如下:
[root@361way queue]# python fifo.py
0
1
2
3
4
输出顺序与输入顺序相同.
2,LIFO(后进先出)
import Queue
q = Queue.LifoQueue()
for i in range(5):
q.put(i)
while not q.empty():
print q.get()
结果如下:
import Queue
q = Queue.LifoQueue()
for i in range(5):
q.put(i)
while not q.empty():
print q.get()
3. 优先排队
import Queue
class Job(object):
def __init__(self, priority, description):
self.priority = priority
self.description = description
print 'New job:', description
return
def __cmp__(self, other):
return cmp(self.priority, other.priority)
q = Queue.PriorityQueue()
q.put( Job(3, 'Mid-level job') )
q.put( Job(10, 'Low-level job') )
q.put( Job(1, 'Important job') )
while not q.empty():
next_job = q.get()
print 'Processing job:', next_job.description
结果如下:
[root@361way queue]# python Queue_priority.py
New job: Mid-level job
New job: Low-level job
New job: Important job
Processing job: Important job
Processing job: Mid-level job
Processing job: Low-level job
从以上执行结果可以看出,设置的优先级值越小,执行得越早. 另外python中怎么导入queue,这里以单线程为例. 在多线程示例中,当多个线程同时获取()项目时,您可以根据优先级决定首先执行哪个任务.
三个,队列和线程
队列的实际使用与线程结合在一起. 以下是一些队列和线程的代码示例:
from Queue import *
from threading import Thread
import sys
'''this function will process the items in the queue, in serial'''
def processor():
while True:
if queue.empty() == True:
print "the Queue is empty!"
sys.exit(1)
try:
job = queue.get()
print "I'm operating on job item: %s"%(job)
queue.task_done()
except:
print "Failed to operate on job"
'''set variables'''
queue = Queue()
threads = 4
'''a list of job items. you would want this to be more advanced,
like reading from a file or database'''
jobs = [ "job1", "job2", "job3" ]
'''iterate over jobs and put each into the queue in sequence'''
#for job in jobs:
for job in range(100):
print "inserting job into the queue: %s"%(job)
queue.put(job)
'''start some threads, each one will process one job from the queue'''
#for i in range(100):
for i in range(threads):
th = Thread(target=processor)
th.setDaemon(True)
th.start()
'''wait until all jobs are processed before quitting'''
queue.join()
应注意,处理器功能中的“ while True: ”行. 如果缺少此行,则当线程(线程)的数量小于队列的数量时python中怎么导入queue,它将在循环的第一轮之后卡住,并且不会执行以下操作. 循环. 因此,添加此行等效于开始一个无限循环,直到所有队列都结束,队列为空,然后循环结束.
示例2:
[root@361way tmp]# python queue-example-1.py
task 0 finished
task 1 finished
task 3 finished
task 2 finished
task 5 finished
task 4 finished
task 6 finished
task 7 finished
task 9 finished
task 8 finished
[root@361way tmp]# more queue-example-1.py
# File: queue-example-1.py
import threading
import Queue
import time, random
WORKERS = 2
class Worker(threading.Thread):
def __init__(self, queue):
self.__queue = queue
threading.Thread.__init__(self)
def run(self):
while 1:
item = self.__queue.get()
if item is None:
break # reached end of queue
# pretend we're doing something that takes 10-100 ms
time.sleep(random.randint(10, 100) / 1000.0)
print "task", item, "finished"
#
# try it
queue = Queue.Queue(0)
for i in range(WORKERS):
Worker(queue).start() # start a worker
for i in range(10):
queue.put(i)
for i in range(WORKERS):
queue.put(None) # add end-of-queue markers
参考页面:
将PyMoTW排队
图书馆书
Python食谱
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-233397-1.html