Python優(yōu)先隊列實現方法示例
更新時間:2017年09月21日 08:55:01 作者:xiecj_2006
這篇文章主要介紹了Python優(yōu)先隊列實現方法,結合實例形式分析了Python優(yōu)先隊列的具體定義與使用方法,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了Python優(yōu)先隊列實現方法。分享給大家供大家參考,具體如下:
1. 代碼
import Queue import threading 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')) def process_job(q): while True: next_job = q.get() print 'Processing job:', next_job.description q.task_done() workers = [threading.Thread(target=process_job,args=(q,)), threading.Thread(target=process_job,args=(q,)),] for w in workers: w.setDaemon(True) w.start() q.join()
2. 執(zhí)行結果
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相關內容可查看本站專題:《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設計有所幫助。
相關文章
Python并發(fā)concurrent.futures和asyncio實例
這篇文章主要介紹了Python并發(fā)concurrent.futures和asyncio實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05Matplotlib直方圖繪制中的參數bins和rwidth的實現
本文主要介紹了Matplotlib直方圖繪制中的參數bins和rwidth的實現,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-02-02