python如何关闭线程
python关闭线程的方法:首先导入threading,定义一个方法;然后定义线程,target指向要执行的方法,启动它;最后停止线程,代码为【stop_thread(myThread)】。
本教程操作环境:windows7系统、python3.9版,DELL G3电脑。
python关闭线程的方法:
一、启动线程
首先导入threading
import threading
然后定义一个方法
def serial_read():
...
...
然后定义线程,target指向要执行的方法
myThread = threading.Thread(target=serial_read)
启动它
myThread.start()
二、停止线程
直接上代码
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)
-
冷钱包是什么?跟热钱包有什么差别?如何选择最安全的存币方式? 时间:2025-04-30
-
什么是欧易Web3钱包?如何创建或导入/管理欧易Web3钱包? 时间:2025-04-30
-
如何通过Gate Web3钱包购买加密货币? 时间:2025-04-30
-
如何从Gate.io转账至Gate Web3钱包?如何将钱包资产转回Gate.io账户? 时间:2025-04-30
-
Gate Web3钱包常见问题汇总 时间:2025-04-30
-
如何使用Gate.io web3钱包进行转账/充值?(App端) 时间:2025-04-30
今日更新
-
html怎样把div放在底部
阅读:23
-
html中块级元素是什么
阅读:26
-
pycharm删除项目方法步骤
阅读:30
-
Composer2.0向下不兼容导致扩展安装出错解决方法
阅读:23
-
python如何判断变量是否是整数
阅读:27
-
python怎么运行py文件
阅读:34
-
《忘川风华录》全文物收集攻略
阅读:19
-
《 妄想山海》犬封村在哪里
阅读:20
-
《妄想山海》怎么移民
阅读:20
-
《我的侠客》黑白棋子获得方法
阅读:22