python3关闭线程的方法-创新互联
创新互联www.cdcxhl.cn八线动态BGP香港云服务器提供商,新人活动买多久送多久,划算不套路!
创新互联建站是一家专业提供南县企业网站建设,专注与成都网站设计、网站制作、html5、小程序制作等业务。10年已为南县众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。这篇文章主要介绍python3关闭线程的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
python3利用异常来退出当前线程。方法:1.利用raise自定义异常,2.当触发函数stop_thread时调用自定义异常进行退出。

利用异常使线程退出代码如下:
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)停止线程
stop_thread(myThread)
补充知识:python threading实现Thread的修改值,开始,运行,停止,并获得内部值
以上是python3关闭线程的方法的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联-成都网站建设公司行业资讯频道!
文章题目:python3关闭线程的方法-创新互联
链接地址:http://www.jxjierui.cn/article/ccicig.html


咨询
建站咨询
