国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python優先隊列實現方法示例

2020-01-04 16:46:47
字體:
來源:轉載
供稿:網友

本文實例講述了Python優先隊列實現方法。分享給大家供大家參考,具體如下:

1. 代碼

import Queueimport threadingclass 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. 執行結果

New job: Mid-level jobNew job: Low-level jobNew job: Important jobProcessing job: Important jobProcessing job: Mid-level jobProcessing job: Low-level job

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乐清市| 辰溪县| 本溪| 富裕县| 绩溪县| 敦化市| 九江县| 达孜县| 凤凰县| 上饶县| 南雄市| 雷波县| 宁南县| 连城县| 大港区| 隆德县| 三亚市| 瓦房店市| 湖州市| 西吉县| 缙云县| 南宫市| 增城市| 囊谦县| 商南县| 尉氏县| 绥阳县| 女性| 远安县| 庆城县| 凤翔县| 康定县| 葫芦岛市| 仁寿县| 阿勒泰市| 三亚市| 册亨县| 彩票| 巨野县| 镇坪县| 阳曲县|