python主要是通过thread和threading这两个模块来实现多线程支持。
python的thread模块是比較底层的模块,python的threading模块是对thread做了一些封装,能够更加方便的被使用。可是python(cpython)因为GIL的存在无法使用threading充分利用CPU资源,假设想充分发挥多核CPU的计算能力须要使用multiprocessing模块(Windows下使用会有诸多问题)。
假设在对线程应用有较高的要求时能够考虑使用Stackless Python来完毕。Stackless Python是Python的一个改动版本号,对多线程编程有更好的支持,提供了对微线程的支持。微线程是轻量级的线程,在多个线程间切换所需的时间很多其它,占用资源也更少。
通过threading模块创建新的线程有两种方法:一种是通过threading.Thread(Target=executable Method)-即传递给Thread对象一个可运行方法(或对象);另外一种是继承threading.Thread定义子类并重写run()方法。另外一种方法中,唯一必须重写的方法是run(),可依据需要决定是否重写__init__()。值得注意的是,若要重写__init__(),父类的__init__()必需要在函数第一行调用,否则会触发错误“AssertionError: Thread.__init__() not called”
Python threading模块不同于其它语言之处在于它没有提供线程的终止方法,通过Python threading.Thread()启动的线程彼此是独立的。若在线程A中启动了线程B,那么A、B是彼此独立执行的线程。若想终止线程A的同一时候强力终止线程B。一个简单的方法是通过在线程A中调用B.setDaemon(True)实现。
但这样带来的问题是:线程B中的资源(打开的文件、传输数据等)可能会没有正确的释放。所以setDaemon()并不是一个好方法,更为妥当的方式是通过Event机制。以下这段程序体现了setDaemon()和Event机制终止子线程的差别。
import threading import time class mythread(threading.Thread): def __init__(self,stopevt = None,File=None,name = 'subthread',Type ='event'): threading.Thread.__init__(self) self.stopevt = stopevt self.name = name self.File = File self.Type = Type def Eventrun(self): while not self.stopevt.isSet(): print self.name +' alive\n' time.sleep(2) if self.File: print 'close opened file in '+self.name+'\n' self.File.close() print self.name +' stoped\n' def Daemonrun(self): D = mythreadDaemon(self.File) D.setDaemon(True) while not self.stopevt.isSet(): print self.name +' alive\n' time.sleep(2) print self.name +' stoped\n' def run(self): if self.Type == 'event': self.Eventrun() else: self.Daemonrun() class mythreadDaemon(threading.Thread): def __init__(self,File=None,name = 'Daemonthread'): threading.Thread.__init__(self) self.name = name self.File = File def run(self): while True: print self.name +' alive\n' time.sleep(2) if self.File: print 'close opened file in '+self.name+'\n' self.File.close() print self.name +' stoped\n' def evtstop(): stopevt = threading.Event() FileA = open('testA.txt','w') FileB = open('testB.txt','w') A = mythread(stopevt,FileA,'subthreadA') B = mythread(stopevt,FileB,'subthreadB') print repr(threading.currentThread())+'alive\n' print FileA.name + ' closed"htmlcode">-------stop subthread example with Event:---------- <_MainThread(MainThread, started 2436)>alive testA.txt closed"font-size: large">总结以上就是本文关于Python线程创建和终止实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
- Atlus《暗喻幻想》媒体评分汇总:高分好评如潮!
- 2024金摇杆奖提名揭晓 《黑神话》角逐最佳视觉设计!
- 群星《新说唱2024 第3期 (上)》[320K/MP3][32.76MB]
- 群星《新说唱2024 第3期 (上)》[FLAC/分轨][95.38MB]
- 群星《新说唱2024 第3期 (下)》[320K/MP3][31.36MB]
- 幻兽帕鲁手游什么时候正式上线 最新消息一览
- 西普大陆BOSS位置盘点 解锁天启纪元玩法
- 西普大陆精灵进阶培养攻略 精灵养成指南
- dnf手游法控法系职业哪个强 dnf手游法控法系职业强度排行
- 魔兽世界血藤护目镜图纸在哪买 wlk血藤护目镜图纸购买位置介绍
- 魔兽世界无畏远征军声望军需官在哪 wlk无畏远征军声望军需官坐标位置
- 逼迫中国选手弃权事件持续发酵 玩家抵制万代模型
- B社老员工在《星空》开发期间离职 他终于说出原因
- 南极正以惊人速度变绿 35年植被突增14倍
- 【原神】完美的4星队伍——砂糖武装(入门攻略)