亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

python 在threading中如何處理主進程和子線程的關系

瀏覽:4日期:2022-07-27 14:57:18

之前用python的多線程,總是處理不好進程和線程之間的關系。后來發現了join和setDaemon函數,才終于弄明白。下面總結一下。

1.使用join函數后,主進程會在調用join的地方等待子線程結束,然后才接著往下執行。

join使用實例如下:

import timeimport randomimport threading class worker(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): t = random.randint(1,10) time.sleep(t) print 'This is ' + self.getName() + ';I sleep %d second.'%(t) tsk = []for i in xrange(0,5): time.sleep(0.1) thread = worker() thread.start() tsk.append(thread)for tt in tsk: tt.join()print 'This is the end of main thread.'

運行結果如下:

# python testjoin.py This is Thread-3;I sleep 2 second.This is Thread-1;I sleep 4 second.This is Thread-2;I sleep 7 second.This is Thread-4;I sleep 7 second.This is Thread-5;I sleep 7 second.This is the end of main thread.

這里創建了5個子線程,每個線程隨機等待1-10秒后打印退出;主線程分別等待5個子線程結束。最后結果是先顯示各個子線程,再顯示主進程的結果。

2. 如果使用的setDaemon函數,則與join相反,主進程結束的時候不會等待子線程。

setDaemon函數使用實例:

import timeimport randomimport threading class worker(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): t = random.randint(1,10) time.sleep(t) print 'This is ' + self.getName() + ';I sleep %d second.'%(t) tsk = []for i in xrange(0,5): time.sleep(0.1) thread = worker() thread.setDaemon(True) thread.start() tsk.append(thread)print 'This is the end of main thread.'

這里設置主進程為守護進程,當主進程結束的時候,子線程被中止

運行結果如下:

#python testsetDaemon.py This is the end of main thread.

3、如果沒有使用join和setDaemon函數,則主進程在創建子線程后,直接運行后面的代碼,主程序一直掛起,直到子線程結束才能結束。

import timeimport randomimport threading class worker(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): t = random.randint(1,10) time.sleep(t) print 'This is ' + self.getName() + ';I sleep %d second.'%(t) tsk = []for i in xrange(0,5): time.sleep(0.1) thread = worker() thread.start() tsk.append(thread)print 'This is the end of main thread.'

運行結果如下:

# python testthread.py This is the end of main thread.This is Thread-4;I sleep 1 second.This is Thread-3;I sleep 7 second.This is Thread-5;I sleep 7 second.This is Thread-1;I sleep 10 second.This is Thread-2;I sleep 10 second.

補充知識:Python Thread和Process對比

原因:進程和線程的差距(方向不同,之針對這個實例)

# coding=utf-8import loggingimport multiprocessingimport osimport timefrom threading import Threadlogging.basicConfig( level=logging.INFO, format='%(asctime)s 【 %(process)d 】 %(processName)s %(message)s')def func (i): # logging.info(f’子:{os.getpid()},t{i}’) return f’子:{os.getpid()},t{i}’def main (ctx): start01 = time.time() ts = [Thread(target=func, args=(i,)) for i in range(100)] [t.start() for t in ts] [t.join() for t in ts] end01 = time.time() - start01 logging.info(f'線程花費的時間:{end01}秒') start02 = time.time() ps = [ctx.Process(target=func, args=(i,)) for i in range(100)] [p.start() for p in ps] [p.join() for p in ps] end02 = time.time() - start02 logging.info(f'進程花費的時間:{end02}秒')if __name__ == ’__main__’: # windows 啟動方式 multiprocessing.set_start_method(’spawn’) # 獲取上下文 ctx = multiprocessing.get_context(’spawn’) # 檢查這是否是凍結的可執行文件中的偽分支進程。 ctx.freeze_support() main(ctx)

輸出:

2019-10-06 14:17:22,729 【 7412 】 MainProcess 線程花費的時間:0.012967586517333984秒2019-10-06 14:17:25,671 【 7412 】 MainProcess 進程花費的時間:2.9418249130249023秒

以上這篇python 在threading中如何處理主進程和子線程的關系就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 国产欧美性综合视频性刺激 | 一级成人a做片免费 | 中文在线观看视频 | 免费人成网站线观看合集 | 自拍 亚洲 欧美 | 日韩欧美国产高清在线观看 | 国产精彩视频在线观看 | 色综合图片二区150p | 极品美女aⅴ高清在线观看 极品美女一级毛片 | 女人被狂躁的视频免费免费看 | 国产成人精品999在线 | 国产一级网站 | 偷拍清纯高清视频在线 | 国产精品你懂得 | 成人久久网 | 九九热精品视频在线播放 | 自拍视频在线观看视频精品 | 在线免费黄色片 | 这里只有精品视频 | 亚洲精品视频免费在线观看 | 青娱乐久草| 久久精品影院一区二区三区 | 99久久精品国产自免费 | 国产毛片片精品天天看视频 | 你懂的在线免费视频 | 亚洲交配视频 | 欧美一级暴毛片 | 亚洲福利午夜 | 精品福利一区二区三区 | 日韩欧美成人乱码一在线 | 国产好大好爽久久久久久久 | 久久一本久综合久久爱 | 国产国拍亚洲精品av | 中文字幕在线影院 | 国产91精品久久久久久 | 91精品国产高清久久久久久 | 久久久精品久久 | 中文字幕啪啪 | 国产视频网 | 韩国一级特黄清高免费大片 | 特及毛片 |