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

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