最新公告
  • 欢迎您光临网站无忧模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • Python中的线程和多线程是什么

    正文概述    2020-04-14   296

    Python中的线程和多线程是什么

    一、线程的概念 

    一个进程里面至少有一个控制线程,进程的概念只是一种抽象的概念,真正在CPU上面调度的是进程里面的线程,就好比真正在地铁这个进程里面工作的实际上是地铁里面的线程,北京地铁里面至少要有一个线程,线程是真正干活的,线程用的是进程里面包含的一堆资源,线程仅仅是一个调度单位,不包含资源。

    什么时候需要开启多个线程:一个进程里面的多个线程共享这个进程里面的资源,因此如果多个任务共享同一块资源的时候,需要开启多个线程。 多线程指的是,在一个进程中开启多个线程,简单的说:如果多个任务共用同一个资源空间,那么必须在一个进程内开启多个线程。一个进程这个任务里面可能对应多个分任务,如果一个进程里面只开启一个线程的话,多个分任务之间实际上是串行的执行效果,即一个程序里面只含有一条执行路径。

    对于计算密集型应用,应该使用多进程;对于IO密集型应用,应该使用多线程。线程的创建比进程的创建开销小的多。

    二、Python中线程的特点 

    1.在其他语言当中,一个进程里面开启多个线程,每个线程都可以给一个cpu去使用,但是在 python当中,在同一时刻,一个进程当中只能有一个线程处于运行状态。

    2.比如在其他语言当中,比如我现在开启了一个进程,这个进程当中含有几个线程,如果我现在有多个cpu,每一个线程是可以对应相应的CPU的。 

    3.但是在python当中,如果我们现在开启了一个进程,这个进程里面对应多个线程,同一时刻只有一个线程可以处于运行状态。 对于其他语言而言,在多CPU系统中,为了最大限度的利用多核,可以开启多个线程。 但是Python中的多线程是利用不了多核优势的。    

    4.在同一个进程当中,多个线程彼此之间可以相互通信;但是进程与进程之间的通信必须基于IPC这种 消息的通信机制(IPC机制包括队列和管道)。在一个进程当中,改变主线程可能会影响其它线程的行为,但是改变父进程并不会影响其它子进程的行为,因为进程与进程之间是完全隔离的。 在python当中,在同一时刻同一进程当中只能同时有一个线程在运行,如果有一个线程使用了系统调用而阻塞,那么整个进程都会被挂起。

    三、多线程的理解

    多进程和多线程都可以执行多个任务,线程是进程的一部分。线程的特点是线程之间可以共享内存和变量,资源消耗少(不过在Unix环境中,多进程和多线程资源调度消耗差距不明显,Unix调度较快),缺点是线程之间的同步和加锁比较麻烦。

    相关推荐:《Python视频教程》

    四、Python多线程创建

    在Python中,同样可以实现多线程,有两个标准模块thread和threading,不过我们主要使用更高级的threading模块。使用例子:

    import threading
    import time
     
    def target():
        print 'the curent threading  %s is running' % threading.current_thread().name
        time.sleep(1)
        print 'the curent threading  %s is ended' % threading.current_thread().name
     
    print 'the curent threading  %s is running' % threading.current_thread().name
    t = threading.Thread(target=target)
     
    t.start()
    t.join()
    print 'the curent threading  %s is ended' % threading.current_thread().name

    输出:

    the curent threading  MainThread is running
    the curent threading  Thread-1 is running
    the curent threading  Thread-1 is ended
    the curent threading  MainThread is ended

    start是启动线程,join是阻塞当前线程,即使得在当前线程结束时,不会退出。从结果可以看到,主线程直到Thread-1结束之后才结束。

    Python中,默认情况下,如果不加join语句,那么主线程不会等到当前线程结束才结束,但却不会立即杀死该线程。如不加join输出如下:

    the curent threading  MainThread is running
    the curent threading  Thread-1 is running
    the curent threading  MainThread is ended
    the curent threading  Thread-1 is ended

    但如果为线程实例添加t.setDaemon(True)之后,如果不加join语句,那么当主线程结束之后,会杀死子线程。

    代码:

    import threading
    import time
    def target():
        print 'the curent threading  %s is running' % threading.current_thread().name
        time.sleep(4)
        print 'the curent threading  %s is ended' % threading.current_thread().name
    print 'the curent threading  %s is running' % threading.current_thread().name
    t = threading.Thread(target=target)
    t.setDaemon(True)
    t.start()
    t.join()
    print 'the curent threading  %s is ended' % threading.current_thread().name

    输出如下:

    the curent threading  MainThread is running
    the curent threading  Thread-1 is runningthe curent threading  MainThread is ended

    如果加上join,并设置等待时间,就会等待线程一段时间再退出:

    import threading
    import time
    def target():
        print 'the curent threading  %s is running' % threading.current_thread().name
        time.sleep(4)
        print 'the curent threading  %s is ended' % threading.current_thread().name
    print 'the curent threading  %s is running' % threading.current_thread().name
    t = threading.Thread(target=target)
    t.setDaemon(True)
    t.start()
    t.join(1)

    输出:

    the curent threading  MainThread is running
    the curent threading  Thread-1 is running
    the curent threading  MainThread is ended

    主线程等待1秒,就自动结束,并杀死子线程。如果join不加等待时间,t.join(),就会一直等待,一直到子线程结束,输出如下:

    the curent threading  MainThread is running
    the curent threading  Thread-1 is running
    the curent threading  Thread-1 is ended
    the curent threading  MainThread is ended

    相关推荐:

    Python中的多进程是什么


    下载网 » Python中的线程和多线程是什么

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元