在python中实现强制关闭线程的示例
如下所示:
importthreading
importtime
importinspect
importctypes
def_async_raise(tid,exctype):
"""raisestheexception,performscleanupifneeded"""
tid=ctypes.c_long(tid)
ifnotinspect.isclass(exctype):
exctype=type(exctype)
res=ctypes.pythonapi.PyThreadState_SetAsyncExc(tid,ctypes.py_object(exctype))
ifres==0:
raiseValueError("invalidthreadid")
elifres!=1:
#"""ifitreturnsanumbergreaterthanone,you'reintrouble,
#andyoushouldcallitagainwithexc=NULLtoreverttheeffect"""
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid,None)
raiseSystemError("PyThreadState_SetAsyncExcfailed")
defstop_thread(thread):
_async_raise(thread.ident,SystemExit)
classTestThread(threading.Thread):
defrun(self):
print
"begin"
whileTrue:
time.sleep(0.1)
print('end')
if__name__=="__main__":
t=TestThread()
t.start()
time.sleep(1)
stop_thread(t)
print('stoped')
以上这篇在python中实现强制关闭线程的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。
热门推荐
10 邻家生小孩祝福语简短
11 开店大吉文案祝福语简短
12 经典送别祝福语大全简短
13 六一婚礼文案祝福语简短
14 离职横幅升职祝福语简短
15 简短的祝福语12字
16 姐妹分手了祝福语简短
17 老人掉牙祝福语简短语
18 祝美术老师祝福语简短