相关文章推荐
不要命的野马  ·  快速入门:从模板创建 Python 项目 ...·  2 天前    · 
光明磊落的葫芦  ·  猫头虎 分享:Python库 Bottle ...·  2 天前    · 
强健的苦瓜  ·  python基础 - 凫弥 ·  昨天    · 
玩篮球的海龟  ·  在 Azure Functions 中排查 ...·  昨天    · 
捣蛋的金针菇  ·  python中StringVar怎么转int ...·  昨天    · 
魁梧的硬币  ·  @document - CSS:层叠样式表 ...·  5 月前    · 
冲动的弓箭  ·  解决报错 ...·  9 月前    · 
威武的登山鞋  ·  thymeleaf th:if ...·  1 年前    · 
侠义非凡的菠菜  ·  rc-form源码浅析 - 知乎·  1 年前    · 
讲道义的匕首  ·  postgresql行号类似oracle ...·  2 年前    · 
Code  ›  Python多线程之线程创建和终止开发者社区
线程 多线程 python多线程 python
https://cloud.tencent.com/developer/article/1570196
开心的棒棒糖
2 年前
作者头像
py3study
0 篇文章

Python多线程之线程创建和终止

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > python3 > Python多线程之线程创建和终止

Python多线程之线程创建和终止

作者头像
py3study
发布 于 2020-01-10 11:14:20
917 0
发布 于 2020-01-10 11:14:20
举报

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? '+repr(FileA.closed)+'\n'   
    print FileB.name + ' closed? '+repr(FileB.closed)+'\n'   
    A.start()   
    B.start()   
    time.sleep(1)   
    print repr(threading.currentThread())+'send stop signal\n'   
    stopevt.set()   
    A.join()   
    B.join()   
    print  repr(threading.currentThread())+'stoped\n'   
    print 'after A stoped, '+FileA.name + ' closed? '+repr(FileA.closed)+'\n'   
    print 'after A stoped, '+FileB.name + ' closed? '+repr(FileB.closed)+'\n'   
def daemonstop():   
    stopevt = threading.Event()   
    FileA = open('testA.txt','r')   
    A = mythread(stopevt,FileA,'subthreadA',Type = 'Daemon')   
    print repr(threading.currentThread())+'alive\n'   
    print FileA.name + ' closed? '+repr(FileA.closed)+'\n'   
    A.start()   
    time.sleep(1)   
    stopevt.set()   
    A.join()   
    print  repr(threading.currentThread())+'stoped\n'   
    print 'after A stoped, '+FileA.name + ' closed? '+repr(FileA.closed)+'\n'   
    if not FileA.closed:   
        print 'You see the differents, the resource in subthread may not released with setDaemon()'   
        FileA.close()   
if __name__ =='__main__':   
    print '-------stop subthread example with Event:----------\n'   
    evtstop()   
    print '-------Daemon stop subthread example :----------\n'   
    daemonstop()  

运行结果是:

-------stop subthread example with Event:----------   
<_MainThread(MainThread, started 2436)>alive   
testA.txt closed? False  
testB.txt closed? False  
subthreadA alive   
subthreadB alive   
<_MainThread(MainThread, started 2436)>send stop signal  
close opened file in subthreadA   
close opened file in subthreadB   
subthreadA stoped   
subthreadB stoped   
<_MainThread(MainThread, started 2436)>stoped   
after A stoped, testA.txt closed? True  
after A stoped, testB.txt closed? True  
-------Daemon stop subthread example :----------   
<_MainThread(MainThread, started 2436)>alive   
 
推荐文章
不要命的野马  ·  快速入门:从模板创建 Python 项目 - Visual Studio (Windows) | Microsoft Learn
2 天前
光明磊落的葫芦  ·  猫头虎 分享:Python库 Bottle 的简介、安装、用法详解入门教程开发者社区
2 天前
强健的苦瓜  ·  python基础 - 凫弥
昨天
玩篮球的海龟  ·  在 Azure Functions 中排查 Python 函数应用错误 | Microsoft Learn
昨天
捣蛋的金针菇  ·  python中StringVar怎么转int_mob649e8158ed1f的技术博客_
昨天
魁梧的硬币  ·  @document - CSS:层叠样式表 | MDN
5 月前
冲动的弓箭  ·  解决报错 /usr/lib64/libz.so.1: version `ZLIB_1.2.9‘ not found (required by /usr/lib64/libpng16.so.16)_li
9 月前
威武的登山鞋  ·  thymeleaf th:if 判断表达式的使用_thymeleaf 不等于-CSDN博客
1 年前
侠义非凡的菠菜  ·  rc-form源码浅析 - 知乎
1 年前
讲道义的匕首  ·  postgresql行号类似oracle rownum效果_pg中替换oracle中 rownum_tanweii163的博客-CSDN博客
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号