python中threading開啟關(guān)閉線程操作
在python中啟動和關(guān)閉線程:
首先導(dǎo)入threading
import threading
然后定義一個(gè)方法
def serial_read():
...
...
然后定義線程,target指向要執(zhí)行的方法
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)
補(bǔ)充知識:python threading實(shí)現(xiàn)Thread的修改值,開始,運(yùn)行,停止,并獲得內(nèi)部值
下面的半模版代碼在 win7+python3.63 運(yùn)行通過并且實(shí)測可行,為了廣大想要實(shí)現(xiàn)python的多線程停止的同學(xué)
import threading
import time
class MyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.Flag=True #停止標(biāo)志位
self.Parm=0 #用來被外部訪問的
#自行添加參數(shù)
def run(self):
while(True):
if(not self.Flag):
break
else:
time.sleep(2)
def setFlag(self,parm): #外部停止線程的操作函數(shù)
self.Flag=parm #boolean
def setParm(self,parm): #外部修改內(nèi)部信息函數(shù)
self.Parm=parm
def getParm(self): #外部獲得內(nèi)部信息函數(shù)
return self.Parm
if __name__=="__main__":
testThread=MyThread()
testThread.setDaemon(True) #設(shè)為保護(hù)線程,主進(jìn)程結(jié)束會關(guān)閉線程
testThread.getParm() #獲得線程內(nèi)部值
testThread.setParm(1) #修改線程內(nèi)部值
testThread.start() #開始線程
print(testThread.getParm()) #輸出內(nèi)部信息
time.sleep(2) #主進(jìn)程休眠 2 秒
testThread.setFlag(False) #修改線程運(yùn)行狀態(tài)
time.sleep(2) #2019.04.25 修改
print(testThread.is_alive()) #查看線程運(yùn)行狀態(tài)
于2018-08-24修正一次,修正為在繼承thread.Thread時(shí),沒有對父類初始化
舊:
def __init__(self):
self.Flag=True #停止標(biāo)志位
self.Parm=0 #用來被外部訪問的
#自行添加參數(shù)
新:
def __init__(self):
threading.Thread.__init__(self)
self.Flag=True #停止標(biāo)志位
self.Parm=0 #用來被外部訪問的
#自行添加參數(shù)
于2019年4月25日進(jìn)行第二次修正,發(fā)現(xiàn)設(shè)置flag值后仍為true輸出的情況,原因是輸出在修改完成前執(zhí)行,睡眠后結(jié)果正常
以上這篇python中threading開啟關(guān)閉線程操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python中threading庫實(shí)現(xiàn)線程鎖與釋放鎖
- Python多線程編程之threading模塊詳解
- Python 多線程之threading 模塊的使用
- python中threading和queue庫實(shí)現(xiàn)多線程編程
- Python threading模塊condition原理及運(yùn)行流程詳解
- Python多線程threading創(chuàng)建及使用方法解析
- Python3 socket即時(shí)通訊腳本實(shí)現(xiàn)代碼實(shí)例(threading多線程)
- Python中使用threading.Event協(xié)調(diào)線程的運(yùn)行詳解
- python繼承threading.Thread實(shí)現(xiàn)有返回值的子類實(shí)例
- 淺談Python中threading join和setDaemon用法及區(qū)別說明
- python threading模塊的使用指南
相關(guān)文章
Python實(shí)現(xiàn)密鑰密碼(加解密)實(shí)例詳解
這篇文章主要介紹了Python實(shí)現(xiàn)密鑰密碼(加解密),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Pytorch實(shí)現(xiàn)張量的創(chuàng)建與使用方法
本文主要介紹了Pytorch實(shí)現(xiàn)張量創(chuàng)建使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-08-08
python開發(fā)實(shí)例之Python的Twisted框架中Deferred對象的詳細(xì)用法與實(shí)例
這篇文章主要介紹了python開發(fā)實(shí)例之Python的Twisted框架中Deferred對象的詳細(xì)用法與實(shí)例,需要的朋友可以參考下2020-03-03
Python中的startswith和endswith函數(shù)使用實(shí)例
這篇文章主要介紹了Python中的startswith和endswith函數(shù)使用實(shí)例,特別是endswith函數(shù),有了它,判斷文件的擴(kuò)展名、文件的類型在容易不過了,需要的朋友可以參考下2014-08-08
用Python將IP地址在整型和字符串之間輕松轉(zhuǎn)換
這篇文章主要給大家介紹了利用Python將IP在整型和字符串之間輕松轉(zhuǎn)換的相關(guān)資料,文中還跟大家分享了Python下利用正則表達(dá)式來匹配校驗(yàn)一個(gè)字符串是否為ip地址的方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-03-03
解決python文件字符串轉(zhuǎn)列表時(shí)遇到空行的問題
下面小編就為大家?guī)硪黄鉀Qpython文件字符串轉(zhuǎn)列表時(shí)遇到空行的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
Python+OpenCV圖像處理——實(shí)現(xiàn)輪廓發(fā)現(xiàn)
這篇文章主要介紹了Python+OpenCV實(shí)現(xiàn)輪廓發(fā)現(xiàn),幫助大家更好的利用python處理圖片,感興趣的朋友可以了解下2020-10-10

