线程优先级队列
2017-11-04 本文已影响11人
NiceBlueChai
Queue模块可以用来进行线程间的通信,让各个线程之间共享数据
Python的Queue模块提供了同步、线程安全的队列类,包括FIFO(先入先出)队列Queue、LIFO(后入后出)队列LifoQueue和优先级队列PriorityQueue。这些队列都实现了锁原语,能够在多线程中直接使用。
可以使用队列实现线程间的同步。
Queue模块中的常用方法
方法名 | 描述 |
---|---|
qsize() | 返回队列大小 |
empty() | 判断队列是否为空,返回True/False |
full() | 判断队列是否已满,返回True/False |
full | 与MaxSize大小对应 |
get([block[,timeout]]) | 获取队列,timeout等待时间 |
get_nowait() | 相当于Queue.get(False) |
put(timeout) | 写入队列,timeout等待时间 |
put_nowait() | 相当于Queue.put(item,False) |
task_done() | 在完成一项工作后,函数向已经完成的队列发送一个信号 |
join() | 实际上意味着等到队列为空,再执行别的操作 |
下面通过实例了解一些方法的使用;
#! /usr/bin/evn python
#-*- coding:utf-8 -*-
import threading
import queue
from time import sleep
class MyThread(threading.Thread):
def __init__(self,threadID,name,q):
threading.Thread.__init__(self)
self.threadID=threadID
self.name=name
self.q=q
def run(self):
print("开启线程:"+self.name)
process_data(self.name,self.q)
print('退出线程:'+self.name)
def process_data(threadName,q):
while not exitFlag:
queueLock.acquire()
if not workQueue.empty():
data=q.get()
queueLock.release()
print('%s processing %s'%(threadName,data))
else:
queueLock.release()
sleep(1)
def main():
global exitFlag
exitFlag=0
threadList=["Thread-1","Thread-2","Thread-3"]
nameList=["One","Two","Three","Four","Five"]
threads=[]
threadID=1
#创建新线程
for tName in threadList:
thread=MyThread(threadID,tName,workQueue)
thread.start()
threads.append(thread)
threadID+=1
#填充队列
queueLock.acquire()
for word in nameList:
workQueue.put(word)
queueLock.release()
#等待清空队列
while not workQueue.empty():
pass
#通知线程退出
exitFlag=1
#等待所有线程完成
for t in threads:
t.join()
print('退出主线程')
if __name__=="__main__":
queueLock=threading.Lock()
workQueue=queue.Queue(10)
main()
执行结果:
开启线程:Thread-1
开启线程:Thread-2
开启线程:Thread-3
Thread-1 processing One
Thread-2 processing Two
Thread-3 processing Three
Thread-2 processing Four
Thread-1 processing Five
退出线程:Thread-3
退出线程:Thread-2
退出线程:Thread-1
❤️
