av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

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

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

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

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

1.使用join函數(shù)后,主進程會在調(diào)用join的地方等待子線程結束,然后才接著往下執(zhí)行。

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.

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

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

setDaemon函數(shù)使用實例:

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函數(shù),則主進程在創(chuàng)建子線程后,直接運行后面的代碼,主程序一直掛起,直到子線程結束才能結束。

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’) # 檢查這是否是凍結的可執(zhí)行文件中的偽分支進程。 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中如何處理主進程和子線程的關系就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 国产女人与拘做受视频 | 日韩区| 国产午夜精品一区二区三区嫩草 | 亚洲一卡二卡 | 精精国产xxxx视频在线野外 | 五月网婷婷 | 国产成人精品免费视频大全最热 | 亚洲一区二区久久久 | 亚洲精品久久久久久首妖 | 久久久久久国产一区二区三区 | 天堂va在线观看 | 爱爱视频网 | 国产精品综合网 | 国产精品日韩欧美一区二区 | 亚洲欧美激情精品一区二区 | 午夜精品一区二区三区免费视频 | 视频一区在线观看 | 免费一看一级毛片 | 国产夜恋视频在线观看 | 激情久久网 | 国产性生活一级片 | 性视频网 | 亚洲免费视频在线观看 | 激情久久久久 | 欧美一区二区三区视频在线播放 | 国内自拍偷拍一区 | 久久国产精品偷 | 天堂中文字幕av | 久久久999国产精品 中文字幕在线精品 | 蜜桃综合在线 | 操久久| 91免费入口 | 国产精品国产精品国产专区不卡 | 久久久久久久国产 | 欧美精品在线一区 | 欧美精品一区二区三 | 午夜影院在线免费观看视频 | 男人天堂99| 欧美精品久久久 | 国产免费a视频 | 久久精品一 |