Python 的 Queue 模塊中提供了同步的、線程安全的隊(duì)列類,包括FIFO(先入先出)隊(duì)列Queue,LIFO(后入先出)隊(duì)列LifoQueue,和優(yōu)先級(jí)隊(duì)列 PriorityQueue。
1、說(shuō)明
這些隊(duì)列都實(shí)現(xiàn)了鎖原語(yǔ),能夠在多線程中直接使用,可以使用隊(duì)列來(lái)實(shí)現(xiàn)線程間的同步。
模塊中的常用方法如下:
- Queue.qsize() 返回隊(duì)列的大小
- Queue.empty() 如果隊(duì)列為空,返回True,反之False
- Queue.full() 如果隊(duì)列滿了,返回True,反之False
- Queue.full 與 maxsize 大小對(duì)應(yīng)
- Queue.get([block[, timeout]])獲取隊(duì)列,timeout等待時(shí)間
- Queue.get_nowait() 相當(dāng)Queue.get(False)
- Queue.put(item) 寫入隊(duì)列,timeout等待時(shí)間
- Queue.put_nowait(item) 相當(dāng)Queue.put(item, False)
- Queue.task_done() 在完成一項(xiàng)工作之后,Queue.task_done()函數(shù)向任務(wù)已經(jīng)完成的隊(duì)列發(fā)送一個(gè)信號(hào)
- Queue.join() 實(shí)際上意味著等到隊(duì)列為空,再執(zhí)行別的操作
2、實(shí)例
#!/usr/bin/python3
import queue
import threading
import time
exitFlag = 0
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()
time.sleep(1)
threadList = ["Thread-1", "Thread-2", "Thread-3"]
nameList = ["One", "Two", "Three", "Four", "Five"]
queueLock = threading.Lock()
workQueue = queue.Queue(10)
threads = []
threadID = 1
# 創(chuàng)建新線程
for tName in threadList:
thread = myThread(threadID, tName, workQueue)
thread.start()
threads.append(thread)
threadID += 1
# 填充隊(duì)列
queueLock.acquire()
for word in nameList:
workQueue.put(word)
queueLock.release()
# 等待隊(duì)列清空
while not workQueue.empty():
pass
# 通知線程是時(shí)候退出
exitFlag = 1
# 等待所有線程完成
for t in threads:
t.join()
print ("退出主線程")
知識(shí)點(diǎn)擴(kuò)展:
問(wèn)題
怎樣實(shí)現(xiàn)一個(gè)按優(yōu)先級(jí)排序的隊(duì)列? 并且在這個(gè)隊(duì)列上面每次 pop 操作總是返回優(yōu)先級(jí)最高的那個(gè)元素
解決方案
下面的類利用 heapq 模塊實(shí)現(xiàn)了一個(gè)簡(jiǎn)單的優(yōu)先級(jí)隊(duì)列:
import heapq
class PriorityQueue:
def __init__(self):
self._queue = []
self._index = 0
def push(self, item, priority):
heapq.heappush(self._queue, (-priority, self._index, item))
self._index += 1
def pop(self):
return heapq.heappop(self._queue)[-1]
下面是它的使用方式:
>>> class Item:
... def __init__(self, name):
... self.name = name
... def __repr__(self):
... return 'Item({!r})'.format(self.name)
...
>>> q = PriorityQueue()
>>> q.push(Item('foo'), 1)
>>> q.push(Item('bar'), 5)
>>> q.push(Item('spam'), 4)
>>> q.push(Item('grok'), 1)
>>> q.pop()
Item('bar')
>>> q.pop()
Item('spam')
>>> q.pop()
Item('foo')
>>> q.pop()
Item('grok')
>>>
到此這篇關(guān)于python線程優(yōu)先級(jí)隊(duì)列知識(shí)點(diǎn)總結(jié)的文章就介紹到這了,更多相關(guān)python線程優(yōu)先級(jí)隊(duì)列有哪些內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 如何通過(guò)Python實(shí)現(xiàn)RabbitMQ延遲隊(duì)列
- python分布式爬蟲中消息隊(duì)列知識(shí)點(diǎn)詳解
- Python通過(guò)隊(duì)列來(lái)實(shí)現(xiàn)進(jìn)程間通信的示例
- Python collections.deque雙邊隊(duì)列原理詳解
- 基于python實(shí)現(xiàn)操作redis及消息隊(duì)列
- Python Celery異步任務(wù)隊(duì)列使用方法解析
- Python實(shí)現(xiàn)一個(gè)優(yōu)先級(jí)隊(duì)列的方法
- Python如何使用隊(duì)列方式實(shí)現(xiàn)多線程爬蟲
- Python多線程通信queue隊(duì)列用法實(shí)例分析
- python3 deque 雙向隊(duì)列創(chuàng)建與使用方法分析
- Python實(shí)現(xiàn)隊(duì)列的方法示例小結(jié)【數(shù)組,鏈表】
- 詳解python數(shù)據(jù)結(jié)構(gòu)之隊(duì)列Queue