在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)
补充知识:python threading实现Thread的修改值,开始,运行,停止,并获得内部值
下面的半模版代码在 win7+python3.63 运行通过并且实测可行,为了广大想要实现python的多线程停止的同学
import threading import time class MyThread(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.Flag=True #停止标志位 self.Parm=0 #用来被外部访问的 #自行添加参数 def run(self): while(True): if(not self.Flag): break else: time.sleep(2) def setFlag(self,parm): #外部停止线程的操作函数 self.Flag=parm #boolean def setParm(self,parm): #外部修改内部信息函数 self.Parm=parm def getParm(self): #外部获得内部信息函数 return self.Parm if __name__=="__main__": testThread=MyThread() testThread.setDaemon(True) #设为保护线程,主进程结束会关闭线程 testThread.getParm() #获得线程内部值 testThread.setParm(1) #修改线程内部值 testThread.start() #开始线程 print(testThread.getParm()) #输出内部信息 time.sleep(2) #主进程休眠 2 秒 testThread.setFlag(False) #修改线程运行状态 time.sleep(2) #2019.04.25 修改 print(testThread.is_alive()) #查看线程运行状态
于2018-08-24修正一次,修正为在继承thread.Thread时,没有对父类初始化
旧:
def __init__(self): self.Flag=True #停止标志位 self.Parm=0 #用来被外部访问的 #自行添加参数
新:
def __init__(self): threading.Thread.__init__(self) self.Flag=True #停止标志位 self.Parm=0 #用来被外部访问的 #自行添加参数
于2019年4月25日进行第二次修正,发现设置flag值后仍为true输出的情况,原因是输出在修改完成前执行,睡眠后结果正常
以上这篇python中threading开启关闭线程操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。
更新日志
- 《优米雅的炼金工房》“妮娜”战斗实机:大雷黑丝金发御姐
- 许冠杰.2003-四合一珍藏集4CD【宝丽金】【WAV+CUE】
- 李宇春.2014-1987我不知会遇见你【天娱传媒】【WAV+CUE】
- 梁汉文.1995-抱着你感觉很好【华星】【WAV+CUE】
- 《情歌回首 HIFI国语老歌 2CD》[WAV/分轨][1.2GB]
- 《降央卓玛 草原绝色醇美的歌声 金色的卓玛》[WAV/分轨][520MB]
- 《赵雷 30首经典城市民谣 2CD》[WAV/分轨][1.3GB]
- 仙境传说新启航兑换码怎么使用 仙境传说兑换码使用方法
- 魔兽世界地心之战武器战用什么饰品 地心之战武器战饰品推荐
- 仙境传说新启航一区是哪个区 仙境传说一区服务器介绍
- 叶倩文2014《影视金曲》LPCD45【WAV+CUE】
- 张国荣1996-哥哥的前半生[华星]2CD[WAV]
- 银霞.1981-《泥泞中的小花·期待》台湾复刻版[WAV+CUE]
- PS掌机游玩《宇宙机器人》体验测试 玩家:这游戏就得用便携模式玩!
- 《黑神话:悟空》角色游戏内形象与86版西游记对比:这版本太经典了