python线程强制停止工作

线程模块没有停止方法,是为了安全,但是我们需要停止子线程呢。小编罗列了,可以有几种安全停止线程的方式,主要涉及的是需要将共享变量作为标志、event对象还有调用C函数接口,但是如果碰到需要去强制停止线程的方式,这就需要我们去调用C函数接口,一起来了解下吧。

强制停止线程,ctypes 调用 C 函数接口

importthreading
importtime
importinspect
importctypes
def_async_raise(tid,exctype):
ifnotinspect.isclass(exctype):
exctype=type(exctype)
elifres!=1:
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid,None)
raiseSystemError("PyThreadState_SetAsyncExcfailed")
defstop_thread(thread):

发表回复