色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

python 在threading中如何處理主進(jìn)程和子線程的關(guān)系

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

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

1.使用join函數(shù)后,主進(jìn)程會在調(diào)用join的地方等待子線程結(jié)束,然后才接著往下執(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.'

運(yùn)行結(jié)果如下:

# 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個子線程,每個線程隨機(jī)等待1-10秒后打印退出;主線程分別等待5個子線程結(jié)束。最后結(jié)果是先顯示各個子線程,再顯示主進(jìn)程的結(jié)果。

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

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.'

這里設(shè)置主進(jìn)程為守護(hù)進(jìn)程,當(dāng)主進(jìn)程結(jié)束的時候,子線程被中止

運(yùn)行結(jié)果如下:

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

3、如果沒有使用join和setDaemon函數(shù),則主進(jìn)程在創(chuàng)建子線程后,直接運(yùn)行后面的代碼,主程序一直掛起,直到子線程結(jié)束才能結(jié)束。

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.'

運(yùn)行結(jié)果如下:

# 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.

補(bǔ)充知識:Python Thread和Process對比

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

# 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'線程花費(fèi)的時間:{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'進(jìn)程花費(fèi)的時間:{end02}秒')if __name__ == ’__main__’: # windows 啟動方式 multiprocessing.set_start_method(’spawn’) # 獲取上下文 ctx = multiprocessing.get_context(’spawn’) # 檢查這是否是凍結(jié)的可執(zhí)行文件中的偽分支進(jìn)程。 ctx.freeze_support() main(ctx)

輸出:

2019-10-06 14:17:22,729 【 7412 】 MainProcess 線程花費(fèi)的時間:0.012967586517333984秒2019-10-06 14:17:25,671 【 7412 】 MainProcess 進(jìn)程花費(fèi)的時間:2.9418249130249023秒

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

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 国产在线播放成人免费 | 2021国产精品一区二区在线 | 久操免费在线视频 | 性欧美欧美巨大69 | 日本久久不射 | 国产成人aa在线观看视频 | 国产美女一级视频 | 欧美一级在线观看视频 | 国产精品综合一区二区 | 亚洲一区二区久久 | 午夜精 | 99热成人精品热久久66 | 美女双腿打开让男人桶爽网站 | 中国a级淫片免费播放 | 久久免费影院 | 国产日韩欧美视频在线 | 国产成人午夜精品免费视频 | 国产一区二区三区高清 | 日韩三级欧美 | jiucao在线观看精品 | 特级无码a级毛片特黄 | 老鸭窝 国产 精品 91 | 日韩免费视频播播 | 国产三香港三韩国三级不卡 | 欧美另类丝袜 | 亚洲欧美中文字幕在线网站 | 亚洲精品国产一区二区三区在 | 亚洲欧美日韩视频一区 | 欧美va免费大片 | 免费看特级毛片 | 亚洲午夜久久久久国产 | 亚州免费视频 | 亚洲波多野结衣日韩在线 | 美女黄页在线观看 | 国产精品99在线观看 | 久久99精品一级毛片 | 最近日本免费观看视频 | 日本韩国一级毛片中文字幕 | 精品成人毛片一区二区视 | 欧美日韩精品在线视频 | 久久精品中文字幕有码日本 |