使用PyThreadState_SetAsyncExc
函数在子线程中引发异常,从而使线程提前结束。
示例代码如下:
import threading
import ctypes
import timeclass thread_with_exception(threading.Thread):
def__init__(self, name):
threading.Thread.__init__(self)
self.name = name
def run(self):
# target function of the thread class try:
while True:
print('running ' + self.name)
finally:
print('ended')
def get_id(self):
# returns id of the respective thread if hasattr(self, '_thread_id'):
return self._thread_id
for id, thread in threading._active.items():
if thread is self:
return id
def raise_exception(self):
thread_id = self.get_id()
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id,
ctypes.py_object(SystemExit))
if res > 1:
ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id, 0)
print('Exception raise failure')
t1 = thread_with_exception('Thread 1')
t1.start()
time.sleep(2)
t1.raise_exception()
t1.join()
把run()
中执行的操作以参数的形式传递:
class KillableThread(threading.Thread):
""" A thread class extending threading.Thread, provides a kill() method to stop the thread and a getResult() method to get the return value of the thread. """def__init__(self, func: Callable, *args, **kwargs):
super().__init__()
self.func: Callable = func
self.funcArg: dict = kwargs
self.funcTup: Tuple = args
self.result: Any = None
self.exception: Any = None
def run(self):
""" Executes the function here """try:
self.result = self.func(*self.funcTup, **self.funcArg)
exceptBaseExceptionas e:
self.exception = e
def get_id(self):
""" Get the id of the thread """if hasattr(self, '_thread_id'):
return self._thread_id
for id, thread in threading._active.items():
if thread is self:
return id
def kill(self):
""" Stops the thread """
thread_id = self.get_id()
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id,
ctypes.py_object(SystemExit))
if res > 1:
ctypes.pythonapi.PyThreadState_SetAsyncExc(thread_id, 0)
print('Exception raise failure')
def getResult(self):
if self.exception is None:
return self.result
else:
raise self.exception
想要了解更多内容,请持续关注码农资源网,一起探索发现编程世界的无限可能!
本站部分资源来源于网络,仅限用于学习和研究目的,请勿用于其他用途。
如有侵权请发送邮件至1943759704@qq.com删除
码农资源网 » Python关闭线程的方法
本站部分资源来源于网络,仅限用于学习和研究目的,请勿用于其他用途。
如有侵权请发送邮件至1943759704@qq.com删除
码农资源网 » Python关闭线程的方法