資訊內容
python如何關閉線程
python關閉線程的方法:首先導入threading,定義一個方法;然后定義線程,target指向要執行的方法,啟動它;**后停止線程,代碼為【stop_thread(myThread)】。yxH少兒編程網-https://www.pxcodes.com
yxH少兒編程網-https://www.pxcodes.com
本教程操作環境:windows7系統、python3.9版,DELL G3電腦。yxH少兒編程網-https://www.pxcodes.com
python關閉線程的方法:yxH少兒編程網-https://www.pxcodes.com
一、啟動線程yxH少兒編程網-https://www.pxcodes.com
首先導入threadingyxH少兒編程網-https://www.pxcodes.com
import threading然后定義一個方法yxH少兒編程網-https://www.pxcodes.com
def serial_read(): ... ...然后定義線程,target指向要執行的方法yxH少兒編程網-https://www.pxcodes.com
myThread = threading.Thread(target=serial_read)啟動它yxH少兒編程網-https://www.pxcodes.com
myThread.start()二、停止線程yxH少兒編程網-https://www.pxcodes.com
不多說了直接上代碼yxH少兒編程網-https://www.pxcodes.com
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)停止線程yxH少兒編程網-https://www.pxcodes.com
stop_thread(myThread)相關免費學習推薦:python視頻教程yxH少兒編程網-https://www.pxcodes.com
以上就是python如何關閉線程的詳細內容,更多請關注少兒編程網其它相關文章!yxH少兒編程網-https://www.pxcodes.com

- 上一篇
python cv2模塊怎么安裝
簡介安裝方法:1、配置python環境;2、打開cmd命令窗口,執行“python”命令檢查環境是否配置好;3、如果配置成功,則執行“exit()”命令;4、執行“pipinstallopencv-python”命令來安裝cv2模塊。本教程操作環境:windows10系統、python3版,DELLG3
- 下一篇
python如何判斷變量是否是整數
簡介python判斷變量是否是整數的方法:1、使用函數【type()】函數可以直接返回變量類型;2、使用【isinstance()】函數可以用來判斷變量的類型,返回的是一個布爾值。本教程操作環境:windows7系統、python3.9版,DELLG3電腦。python判斷變量是否是整數的方法:pyth