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

首頁 > 編程 > Python > 正文

python 多線程應用介紹

2020-02-23 04:50:24
字體:
來源:轉載
供稿:網友

python可以方便地支持多線程??梢钥焖賱摻ň€程、互斥鎖、信號量等等元素,支持線程讀寫同步互斥。美中不足的是,python的運行在python 虛擬機上,創建的多線程可能是虛擬的線程,需要由python虛擬機來輪詢調度,這大大降低了python多線程的可用性。我們經今天用了經典的生產者和消費者的問題來說明下python的多線程的運用 上代碼:

#encoding=utf-8 import threading import random import time from Queue import Queue class Producer(threading.Thread): def __init__(self, threadname, queue): threading.Thread.__init__(self, name = threadname) self.sharedata = queue def run(self): for i in range(20): print self.getName(),'adding',i,'to queue' self.sharedata.put(i) time.sleep(random.randrange(10)/10.0) print self.getName(),'Finished' # Consumer thread class Consumer(threading.Thread): def __init__(self, threadname, queue): threading.Thread.__init__(self, name = threadname) self.sharedata = queue def run(self): for i in range(20): print self.getName(),'got a value:',self.sharedata.get() time.sleep(random.randrange(10)/10.0) print self.getName(),'Finished' # Main thread def main(): queue = Queue() producer = Producer('Producer', queue) consumer = Consumer('Consumer', queue) print 'Starting threads ...' producer.start() consumer.start() producer.join() consumer.join() print 'All threads have terminated.' if __name__ == '__main__': main() 

你親自運行下這斷代碼,可能有不一樣的感覺!理解以后可以用python cookielib 再結果python urllib 寫一個多線程下載網頁的腳本應該沒什么問題

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新建县| 获嘉县| 巴青县| 巍山| 郑州市| 正定县| 龙川县| 老河口市| 河曲县| 长阳| 银川市| 临西县| 中卫市| 巴林左旗| 瓦房店市| 孟津县| 清徐县| 武陟县| 黄浦区| 南汇区| 内黄县| 保靖县| 邻水| 德清县| 彰武县| 铅山县| 墨江| 平湖市| 云南省| 吉林省| 积石山| 六安市| 焉耆| 恩平市| 成安县| 亳州市| 青川县| 仙桃市| 巴林右旗| 家居| 虎林市|