您好,登錄后才能下訂單哦!
今天小編給大家分享一下Python隊列的使用方法有哪些的相關(guān)知識點,內(nèi)容詳細(xì),邏輯清晰,相信大部分人都還太了解這方面的知識,所以分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后有所收獲,下面我們一起來了解一下吧。
此模塊適用于線程間通信,但不能用于進(jìn)程間通信。
示例代碼1: 【注意:此時代碼存在錯誤?。?!】
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é)束!")
注意:上述寫法:
while queue.qsize() > 0: x = queue.get()
當(dāng)生產(chǎn)者速度沒有消費者速度快時,上述消費者代碼會提前結(jié)束,導(dǎo)致生產(chǎn)者的速度不能消費。
while True: x = queue.get()
這種寫法也存在問題,此時消費者隊列會一直監(jiān)聽生產(chǎn)者隊列是否有數(shù)據(jù),導(dǎo)致線程一直處于阻塞狀態(tài),程序會一直阻塞不會停止,嚴(yán)重浪費系統(tǒng)資源。如果使用apscheduler等定時任務(wù)的庫的話,會導(dǎo)致定時任務(wù)無法啟動。
其實queue隊列中的put()或者get()方法中都提供了timeout參數(shù),利用這個參數(shù)可以有效解決上述消除不能消費和線程一直阻塞問題。
示例代碼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é)束!")
運行結(jié)果:
根據(jù)不同的情境,可以根據(jù)實際情況設(shè)置timeout的值。如果使用定時任務(wù),使用timeout是可以的,不會使程序拋異常停止的。
此模塊用于對進(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()會報錯 # 使用multiprocessing中的Queue(),正確輸出a
運行結(jié)果:
示例代碼:
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()
運行結(jié)果:
以上就是“Python隊列的使用方法有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家閱讀完這篇文章都有很大的收獲,小編每天都會為大家更新不同的知識,如果還想學(xué)習(xí)更多的知識,請關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。