關(guān)于Python中幾種隊(duì)列Queue用法區(qū)別
python中使用到的隊(duì)列模塊大致有三個(gè):
1、from queue import Queue
此模塊適用于線程間通信,但不能用于進(jìn)程間通信。
示例代碼1: 【注意:此時(shí)代碼存在錯(cuò)誤?。?!】
import time
import threading
from queue import Queue
def task_func():
global queue
while queue.qsize() > 0:
x = queue.get()
print(f"num: {x}")
time.sleep(0.1)
def producer_data():
global queue
for i in range(100):
queue.put(i)
time.sleep(0.1)
if __name__ == '__main__':
queue = Queue()
producer_thread = threading.Thread(target=producer_data)
producer_thread.start()
thread_list = []
for i in range(5):
thread = threading.Thread(target=task_func)
thread.start()
thread_list.append(thread)
for thread in thread_list:
thread.join()
print("主程序執(zhí)行結(jié)束!")注意:上述寫(xiě)法:
while queue.qsize() > 0:
x = queue.get()當(dāng)生產(chǎn)者速度沒(méi)有消費(fèi)者速度快時(shí),上述消費(fèi)者代碼會(huì)提前結(jié)束,導(dǎo)致生產(chǎn)者的速度不能消費(fèi)。
while True:
x = queue.get()這種寫(xiě)法也存在問(wèn)題,此時(shí)消費(fèi)者隊(duì)列會(huì)一直監(jiān)聽(tīng)生產(chǎn)者隊(duì)列是否有數(shù)據(jù),導(dǎo)致線程一直處于阻塞狀態(tài),程序會(huì)一直阻塞不會(huì)停止,嚴(yán)重浪費(fèi)系統(tǒng)資源。如果使用apscheduler等定時(shí)任務(wù)的庫(kù)的話,會(huì)導(dǎo)致定時(shí)任務(wù)無(wú)法啟動(dòng)。
其實(shí)queue隊(duì)列中的put()或者get()方法中都提供了timeout參數(shù),利用這個(gè)參數(shù)可以有效解決上述消除不能消費(fèi)和線程一直阻塞問(wèn)題。
示例代碼2:
import time
import threading
from queue import Queue
def task_func():
global queue
while True:
x = queue.get(timeout=10)
print(f"num: {x}")
def producer_data():
global queue
for i in range(100):
queue.put(i)
time.sleep(0.1)
if __name__ == '__main__':
queue = Queue()
producer_thread = threading.Thread(target=producer_data)
producer_thread.start()
thread_list = []
for i in range(5):
thread = threading.Thread(target=task_func)
thread.start()
thread_list.append(thread)
for thread in thread_list:
thread.join()
print("主程序執(zhí)行結(jié)束!")運(yùn)行結(jié)果:

根據(jù)不同的情境,可以根據(jù)實(shí)際情況設(shè)置timeout的值。如果使用定時(shí)任務(wù),使用timeout是可以的,不會(huì)使程序拋異常停止的。
2、from multiprocessing import Queue
此模塊用于對(duì)進(jìn)程,但是不能用于進(jìn)程池
示例代碼:
import time
from multiprocessing import Process, Queue
import queue
def producer(queue):
queue.put("a")
time.sleep(2)
def consumer(queue):
time.sleep(2)
data = queue.get()
print(data)
if __name__ == "__main__":
# queue = queue.Queue()
queue = Queue()
my_producer = Process(target=producer, args=(queue, ))
my_consumer = Process(target=consumer, args=(queue, ))
my_producer.start()
my_consumer.start()
my_producer.join()
my_consumer.join()
# 使用queue模塊的Queue()會(huì)報(bào)錯(cuò)
# 使用multiprocessing中的Queue(),正確輸出a
運(yùn)行結(jié)果:

3、from multiprocessing import Manager
示例代碼:
import time
from multiprocessing import Process, Queue, Pool, Manager
def producer(queue):
queue.put("a")
time.sleep(2)
def consumer(queue):
time.sleep(2)
data = queue.get()
print(data)
if __name__ == "__main__":
# queue = Queue()
queue = Manager().Queue()
pool = Pool()
# pool中的進(jìn)程間通信需要使用Manager
pool.apply_async(producer, args=(queue, ))
pool.apply_async(consumer, args=(queue, ))
pool.close()
pool.join()運(yùn)行結(jié)果:

到此這篇關(guān)于關(guān)于Python中幾種隊(duì)列Queue用法區(qū)別的文章就介紹到這了,更多相關(guān)Python中的隊(duì)列Queue內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python?queue模塊功能大全
- python隊(duì)列queue模塊詳解
- Python Queue模塊詳細(xì)介紹及實(shí)例
- Python Queue模塊詳解
- Python隊(duì)列Queue超詳細(xì)講解
- python中的queue隊(duì)列類型及函數(shù)用法
- python3?queue多線程通信
- python 工具類之Queue組件詳解用法
- Python多線程 Queue 模塊常見(jiàn)用法
- 詳解python數(shù)據(jù)結(jié)構(gòu)之隊(duì)列Queue
- python3爬蟲(chóng)中引用Queue的實(shí)例講解
- Python queue模塊的用法
相關(guān)文章
scipy.interpolate插值方法實(shí)例講解
這篇文章主要介紹了scipy.interpolate插值方法介紹,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
Python 腳本獲取ES 存儲(chǔ)容量的實(shí)例
今天小編就為大家分享一篇Python 腳本獲取ES 存儲(chǔ)容量的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
Biblibili視頻投稿接口分析并以Python實(shí)現(xiàn)自動(dòng)投稿功能
這篇文章主要介紹了Biblibili視頻投稿接口分析并以Python實(shí)現(xiàn)自動(dòng)投稿功能,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
Python使用Docling庫(kù)玩轉(zhuǎn)文檔處理
Docling?是一個(gè)強(qiáng)大的?Python?第三方庫(kù),專注于文檔處理和轉(zhuǎn)換,所以本文將帶大家深入了解?Docling?的強(qiáng)大功能,展示它如何幫助我們高效處理文檔,感興趣的可以了解下2025-02-02
OpenCV Python實(shí)現(xiàn)拼圖小游戲
這篇文章主要為大家詳細(xì)介紹了OpenCV Python實(shí)現(xiàn)拼圖版小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03
python pyg2plot的原理知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于python pyg2plot的原理知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以參考下。2021-02-02

