在python中實現(xiàn)強制關(guān)閉線程的示例
更新時間:2019年01月22日 15:24:44 作者:tian544556
今天小編就為大家分享一篇在python中實現(xiàn)強制關(guān)閉線程的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
如下所示:
import threading import time import inspect import ctypes 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) class TestThread(threading.Thread): def run(self): print "begin" while True: time.sleep(0.1) print('end') if __name__ == "__main__": t = TestThread() t.start() time.sleep(1) stop_thread(t) print('stoped')
以上這篇在python中實現(xiàn)強制關(guān)閉線程的示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
MxNet預(yù)訓(xùn)練模型到Pytorch模型的轉(zhuǎn)換方式
這篇文章主要介紹了MxNet預(yù)訓(xùn)練模型到Pytorch模型的轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05python3實現(xiàn)名片管理系統(tǒng)(控制臺版)
這篇文章主要為大家詳細介紹了python3實現(xiàn)名片管理系統(tǒng)控制臺版,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-11-11python庫umap有效地揭示高維數(shù)據(jù)的結(jié)構(gòu)和模式初探
這篇文章主要介紹了python庫umap有效地揭示高維數(shù)據(jù)的結(jié)構(gòu)和模式初探,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01

Python基于回溯法子集樹模板實現(xiàn)圖的遍歷功能示例
這篇文章主要介紹了Python基于回溯法子集樹模板實現(xiàn)圖的遍歷功能,結(jié)合實例形式分析了Python使用回溯法子集樹模板針對圖形遍歷問題的相關(guān)操作技巧與注意事項,需要的朋友可以參考下
2017-09-09