python子線程退出及線程退出控制的代碼
下面通過代碼給大家介紹python子線程退出問題,具體內(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
跑起來是沒有問題的,但是使用ctrl + c中斷的時候出問題了,主線程退出了,但子線程仍然運行。
于是在主線程增加了信號處理的代碼,收到sigint時改變子線程循環(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時,改變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就可以退出了,但是疑惑的是,主線程退出進程不會退出嗎?
知識點擴展Python線程退出控制
ctypes模塊控制線程退出
Python中threading模塊并沒有設(shè)計線程退出的機制,原因是不正常的線程退出可能會引發(fā)意想不到的后果。
例如:
線程正在持有一個必須正確釋放的關(guān)鍵資源,鎖。
線程創(chuàng)建的子線程,同時也將被殺掉。
管理自己的線程,最好的處理方式是擁有一個請求退出標志,這樣每個線程依據(jù)一定的時間間隔檢查規(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)該定期檢查停止標志,在退出的時候,可以調(diào)用stop()函數(shù),并且使用join()函數(shù)來等待線程的退出。
然而,可能會出現(xiàn)確實想要殺掉線程的情況,例如你正在封裝一個外部庫,它會忙于長時間調(diào)用,而你想中斷它。
Python線程可以拋出異常來結(jié)束:
傳參分別是線程id號和退出標識
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īng)用在實際使用中來進行線程中斷,例如檢測到線程運行時常超過本身可以忍受的范圍。
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子線程退出及線程退出控制的代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
相關(guān)文章
python+pyqt5實現(xiàn)KFC點餐收銀系統(tǒng)
這篇文章主要為大家詳細介紹了python+pyqt5實現(xiàn)KFC點餐收銀系統(tǒng),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01
基于Python利用Pygame實現(xiàn)翻轉(zhuǎn)圖像
這篇文章主要介紹了基于Python利用Pygame實現(xiàn)翻轉(zhuǎn)圖像,我們將了解如何使用Pygame翻轉(zhuǎn)圖像,要翻轉(zhuǎn)圖像,我們需要使用pygame.transform.flip(Surface,?xbool,?ybool)?方法,該方法被調(diào)用來根據(jù)我們的需要在垂直方向或水平方向翻轉(zhuǎn)圖像,下面來看看具體的實現(xiàn)過程吧2022-02-02
PyTorch中的train()、eval()和no_grad()的使用
本文主要介紹了PyTorch中的train()、eval()和no_grad()的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-04-04
tensorflow獲取預訓練模型某層參數(shù)并賦值到當前網(wǎng)絡(luò)指定層方式
今天小編就為大家分享一篇tensorflow獲取預訓練模型某層參數(shù)并賦值到當前網(wǎng)絡(luò)指定層方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01

