久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

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

Python多線程threading join和守護(hù)線程setDeamon原理詳解

瀏覽:4日期:2022-08-02 08:53:41

同一進(jìn)程下的多個線程共享內(nèi)存數(shù)據(jù),多個線程之間沒有主次關(guān)系,相互之間可以操作;cpu執(zhí)行的都是線程,默認(rèn)程序會開一個主線程;進(jìn)程是程序以及和程序相關(guān)資源的集合;某些場景下我們可以使用多線程來達(dá)到提高程序執(zhí)行效率的目的,下面就多線程的一些基礎(chǔ)知識做簡要說明

簡單的多線程

import threading, timedef test1(x): time.sleep(5) print(x**x)#下面定義兩個線程調(diào)用test1這個函數(shù),創(chuàng)建多線程使用如下語法,target后面跟函數(shù)名,args傳遞實參,實參需要以元組形式傳遞start_time = time.time()t1 = threading.Thread(target=test1, args=(5,))t2 = threading.Thread(target=test1, args=(6,))#啟動多線程t1.start()t2.start()end_time = time.time()total_time = end_time - start_timeprint('two Thread used %s time'%total_time) #由于使用多線程,t1 t2啟動以后并不會等待期執(zhí)行完程序才繼續(xù)往后走,因為主程序就是主線程和t1 t2是并行執(zhí)行的,主程序執(zhí)行到此t1 t2并未運行完成time.sleep(6)#多線程啟動數(shù)量比較多時可以使用for循環(huán),多線程并行執(zhí)行,打印的結(jié)果有可能不是按照啟動順序來打印的for i in range(5): t3 = threading.Thread(target=test1, args=(i,)) t3.start()time.sleep(6)

主線程等待非主線程執(zhí)行完畢才繼續(xù)執(zhí)行 join方法

#有些情況主線程需要子線程執(zhí)行完畢后,有可能是將數(shù)據(jù)處理完畢后才執(zhí)行接下來的主線程的東西start_time1 = time.time()tl = [] #將多線程的對象存起來,用于后面join方法for i in range(5): t4 = threading.Thread(target=test1, args=(i,)) t4.start() tl.append(t4)for t in tl: #將多線程并發(fā)join,參加join的子線程執(zhí)行完畢后才繼續(xù)執(zhí)行下面的主線程。 t.join()end_time1 = time.time()total_time1 = end_time1 - start_time1print(total_time1) #此次執(zhí)行時間大約就是5s

#如果多個子線程一些join一些沒有join主線程怎么處理???部分子線程join主線程會等join時間最長的子線程結(jié)束后才繼續(xù),未參與join的子線程仍然和主線程并行運行t5 = threading.Thread(target=test1, args=(5,))t6 = threading.Thread(target=test1, args=(6,))t5.start()t6.start()t5_join_start_time = time.time()t5.join()time.sleep(10)t5_join_end_time = time.time()print('t5 join time is %s'%(t5_join_end_time - t5_join_start_time)) #實際耗時15s

守護(hù)線程 setDeamon

#守護(hù)進(jìn)程,即主線程結(jié)束以后所有的其它線程也立即結(jié)束,不用等其它線程執(zhí)行完畢;正常情況即使沒加join主線程執(zhí)行完畢當(dāng)其它線程未執(zhí)行完畢程序也不會退出,必須等待所有線程執(zhí)行完畢程序才結(jié)束,類似主程序在末尾有默認(rèn)的joindef test1(x): time.sleep(5) print('i an other Thread',x**x)for i in range(5): t = threading.Thread(target=test1, args=(i,)) t.setDaemon(True) t.start()print('Main Thread is done') #整個程序結(jié)束,不會等待守護(hù)線程打印操作執(zhí)行完畢就直接結(jié)束了

遞歸鎖 Rlock

#遞歸鎖,一個鎖里面嵌套著鎖,如果不使用遞歸鎖會導(dǎo)致釋放鎖邏輯錯誤,整個程序就跑偏了;使用遞歸鎖后程序會維護(hù)一個加鎖 解鎖的數(shù)據(jù)結(jié)構(gòu),保證釋放鎖不會出問題lock = threading.Lock()def test2(): lock.acquire() print('this is test2') lock.release()def test3(): lock.acquire() print('this is test3') lock.release()def test4(): lock.acquire() test2() print('this is test4') test3() lock.release()rlock_test = threading.Thread(target=test4)rlock_test.start()while threading.active_count() != 1: print('current thread count is',threading.active_count()) #整個程序一直在打印有兩個線程,非主線程的鎖嵌套出問題導(dǎo)致無法退出,整個程序卡死 time.sleep(1)

將lock = threading.Lock()修改為lock = threading.RLock()整個程序就能正常結(jié)束;正常結(jié)束的輸出如下

this is test2this is test4current thread count is 2this is test3

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 邵阳县| 修文县| 株洲县| 河东区| 澄迈县| 南靖县| 绥阳县| 五家渠市| 东方市| 东安县| 扬中市| 鹿泉市| 锡林浩特市| 公安县| 白沙| 水城县| 富民县| 咸阳市| 张家口市| 华坪县| 迭部县| 合肥市| 文成县| 苍梧县| 浦东新区| 南涧| 满洲里市| 宣城市| 当涂县| 海伦市| 离岛区| 玉溪市| 黔江区| 西城区| 鱼台县| 鄯善县| 荃湾区| 射阳县| 垫江县| 洞口县| 水富县|