python子線程退出及線程退出控制的代碼
下面通過(guò)代碼給大家介紹python子線程退出問(wèn)題,具體內(nèi)容如下所示:
def thread_func(): while True: #do something #do something #do something t=threading.Thread(target = thread_func) t.start() # main thread do something # main thread do something # main thread do something
跑起來(lái)是沒有問(wèn)題的,但是使用ctrl + c中斷的時(shí)候出問(wèn)題了,主線程退出了,但子線程仍然運(yùn)行。
于是在主線程增加了信號(hào)處理的代碼,收到sigint時(shí)改變子線程循環(huán)條件
loop = True def thread_func(): while loop: #do something #do something #do something t=threading.Thread(target = thread_func) t.start() # ctrl+c時(shí),改變loop為False def handler(signum, frame): global loop loop = False t.join() exit(0) signal(SIGINT, handler) # main thread do something # main thread do something # main thread do something
這樣ctrl+c就可以退出了,但是疑惑的是,主線程退出進(jìn)程不會(huì)退出嗎?
知識(shí)點(diǎn)擴(kuò)展Python線程退出控制
ctypes模塊控制線程退出
Python中threading模塊并沒有設(shè)計(jì)線程退出的機(jī)制,原因是不正常的線程退出可能會(huì)引發(fā)意想不到的后果。
例如:
線程正在持有一個(gè)必須正確釋放的關(guān)鍵資源,鎖。
線程創(chuàng)建的子線程,同時(shí)也將被殺掉。
管理自己的線程,最好的處理方式是擁有一個(gè)請(qǐng)求退出標(biāo)志,這樣每個(gè)線程依據(jù)一定的時(shí)間間隔檢查規(guī)則,看是不是需要退出。
例如下面的代碼:
import threading class StoppableThread(threading.Thread): """Thread class with a stop() method. The thread itself has to check regularly for the stopped() condition.""" def __init__(self): super(StoppableThread, self).__init__() self._stop_event = threading.Event() def stop(self): self._stop_event.set() def stopped(self): return self._stop_event.is_set()
這段代碼里面,線程應(yīng)該定期檢查停止標(biāo)志,在退出的時(shí)候,可以調(diào)用stop()函數(shù),并且使用join()函數(shù)來(lái)等待線程的退出。
然而,可能會(huì)出現(xiàn)確實(shí)想要?dú)⒌艟€程的情況,例如你正在封裝一個(gè)外部庫(kù),它會(huì)忙于長(zhǎng)時(shí)間調(diào)用,而你想中斷它。
Python線程可以拋出異常來(lái)結(jié)束:
傳參分別是線程id號(hào)和退出標(biāo)識(shí)
def _async_raise(tid, exctype): '''Raises an exception in the threads with id tid''' if not inspect.isclass(exctype): raise TypeError("Only types can be raised (not instances)") res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype)) if res == 0: raise ValueError("invalid thread id") elif res != 1: # "if it returns a number greater than one, you're in trouble, # and you should call it again with exc=NULL to revert the effect" ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, 0) raise SystemError("PyThreadState_SetAsyncExc failed")
如果線程在python解釋器外運(yùn)行時(shí),它將不會(huì)捕獲中斷,即拋出異常后,不能對(duì)線程進(jìn)行中斷。
簡(jiǎn)化后,以上代碼可以應(yīng)用在實(shí)際使用中來(lái)進(jìn)行線程中斷,例如檢測(cè)到線程運(yùn)行時(shí)常超過(guò)本身可以忍受的范圍。
def _async_raise(tid, exctype): """raises the exception, performs cleanup if needed""" tid = ctypes.c_long(tid) if not inspect.isclass(exctype): exctype = type(exctype) res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype)) if res == 0: raise ValueError("invalid thread id") elif res != 1: # """if it returns a number greater than one, you're in trouble, # and you should call it again with exc=NULL to revert the effect""" ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None) raise SystemError("PyThreadState_SetAsyncExc failed") def stop_thread(thread): _async_raise(thread.ident, SystemExit)
總結(jié)
以上所述是小編給大家介紹的python子線程退出及線程退出控制的代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- Python控制線程和函數(shù)超時(shí)處理
- python如何控制進(jìn)程或者線程的個(gè)數(shù)
- python多線程semaphore實(shí)現(xiàn)線程數(shù)控制的示例
- Python多線程的退出控制實(shí)現(xiàn)
- python基于event實(shí)現(xiàn)線程間通信控制
- python多線程同步之文件讀寫控制
- python自定義線程池控制線程數(shù)量的示例
- Python多線程同步---文件讀寫控制方法
- python簡(jiǎn)易遠(yuǎn)程控制單線程版
- Python控制多進(jìn)程與多線程并發(fā)數(shù)總結(jié)
- Python多線程中線程數(shù)量如何控制
相關(guān)文章
python?selenium在打開的瀏覽器中動(dòng)態(tài)調(diào)整User?Agent
這篇文章主要介紹的是python?selenium在打開的瀏覽器中動(dòng)態(tài)調(diào)整User?Agent,具體相關(guān)資料請(qǐng)需要的朋友參考下面文章詳細(xì)內(nèi)容,希望對(duì)你有所幫助2022-02-02Python實(shí)現(xiàn)信用卡系統(tǒng)(支持購(gòu)物、轉(zhuǎn)賬、存取錢)
這篇文章主要介紹了Python實(shí)現(xiàn)信用卡系統(tǒng)(支持購(gòu)物、轉(zhuǎn)賬、存取錢)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06python 在服務(wù)器上調(diào)用數(shù)據(jù)庫(kù)特別慢的解決過(guò)程
這篇文章主要介紹了python 在服務(wù)器上調(diào)用數(shù)據(jù)庫(kù)特別慢的解決過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04解決python中使用plot畫圖,圖不顯示的問(wèn)題
今天小編就為大家分享一篇解決python中使用plot畫圖,圖不顯示的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07深入分析在Python模塊頂層運(yùn)行的代碼引起的一個(gè)Bug
幾個(gè)星期前, 我的同事跑過(guò)來(lái), 說(shuō)發(fā)現(xiàn)一個(gè)奇怪的Bug: 在使用Python的subprocess運(yùn)行子進(jìn)程時(shí), 當(dāng)子進(jìn)程運(yùn)行失敗時(shí)居然沒有拋出錯(cuò)誤!2014-07-07python正則匹配抓取豆瓣電影鏈接和評(píng)論代碼分享
抓取豆瓣各類型電影的鏈接和評(píng)論,按評(píng)分排列2013-12-12pycharm設(shè)置默認(rèn)的UTF-8編碼模式的方法詳解
這篇文章主要介紹了pycharm設(shè)置默認(rèn)的UTF-8編碼模式,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06