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

首頁 > 編程 > Python > 正文

Python優(yōu)先隊(duì)列實(shí)現(xiàn)方法示例

2020-02-16 10:17:08
字體:
供稿:網(wǎng)友

本文實(shí)例講述了Python優(yōu)先隊(duì)列實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:

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. 執(zhí)行結(jié)果

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

更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿克苏市| 巫山县| 商水县| 信宜市| 许昌县| 安龙县| 冕宁县| 梓潼县| 云龙县| 英山县| 永新县| 滦平县| 石阡县| 杨浦区| 江山市| 青浦区| 工布江达县| 平利县| 罗定市| 孟州市| 隆安县| 金平| 湘潭市| 德钦县| 姜堰市| 洛扎县| 容城县| 茶陵县| 甘肃省| 千阳县| 元朗区| 枣强县| 承德县| 枣阳市| 长岛县| 凌源市| 湟中县| 长宁县| 福清市| 郁南县| 长寿区|