zoukankan      html  css  js  c++  java
  • Python-24-多线程

    环境

    ubuntu 16.04

    anaconda

    pycharm

    python3.6

    https://www.cnblogs.com/jokerbj/p/7460260.html

    多线程 VS 多进程

    程序:一堆代码以文本形式存入一个文档。

    进程:程序运行的一个状态。

      包含地址空间,内训,数据栈等。

      每个进程有自己完全独立的运行环境,多进程共享数据是一个问题。

    线程:

      一个进程的独立运行片段,一个进程可以有多个线程

      线程可以理解为轻量化的进程

      一个进程的多个线程间共享数据和上下文运行环境

      线程会有共享互斥问题

    全局解释器锁(GIL)

      Python代码的执行是由python虚拟机进行控制。

      在主循环中只能有一个控制线程在执行。

      

    Python包

      thread:有问题,不好用,python3改成了_thread

      threading:通用的包

      

    案例01:顺序执行,耗时比较长

    '''
    利用time函数,生成两个函数
    顺序调用
    计算总的运行时间
    '''
    import time
    def loop1():
        # ctime 得到当前的时间
        print('Start loop 1 at : ',time.ctime())
        #睡眠多长时间 单位是秒
        time.sleep(4)
        print('End loop 1 at : ',time.ctime())
    
    
    def loop2():
        # ctime 得到当前的时间
        print('Start loop 2 at : ',time.ctime())
        #睡眠多长时间 单位是秒
        time.sleep(2)
        print('End loop 2 at : ',time.ctime())
    
    def main():
        print('Starting at : ',time.ctime())
        loop1()
        loop2()
        print('All done at : ',time.ctime())
    
    if __name__ == "__main__":
        main()

    运行结果:

    Starting at :  Sun Oct 28 13:17:59 2018
    Start loop 1 at :  Sun Oct 28 13:17:59 2018
    End loop 1 at :  Sun Oct 28 13:18:03 2018
    Start loop 2 at :  Sun Oct 28 13:18:03 2018
    End loop 2 at :  Sun Oct 28 13:18:05 2018
    All done at :  Sun Oct 28 13:18:05 2018
    Starting at 和 All done at 执行的时间差大概是 6秒,因为loop1 耗时4秒,loop2 耗时2秒,顺序执行。

    案例02:改用多线程,缩短总时间,使用 _thread

    '''
    利用time函数,生成两个函数
    计算总的运行时间
    '''
    import time
    import _thread as thread
    def loop1():
        # ctime 得到当前的时间
        print('Start loop 1 at : ',time.ctime())
        #睡眠多长时间 单位是秒
        time.sleep(4)
        print('End loop 1 at : ',time.ctime())
    
    
    def loop2():
        # ctime 得到当前的时间
        print('Start loop 2 at : ',time.ctime())
        #睡眠多长时间 单位是秒
        time.sleep(2)
        print('End loop 2 at : ',time.ctime())
    
    
    def main():
        print('Starting at : ',time.ctime())
        #启动多线程的意思是用多线程去执行某个函数
        #启动多线程函数为start_new_thread
        #参数两个:一个是需要运行的函数名,第二个是函数的参数作为元组使用,为空则使用空元组
        #注意:如果函数只有一个参数,需要参数后有有一个逗号
        thread.start_new_thread(loop1,())
        thread.start_new_thread(loop2,())
    
        print('All done at : ',time.ctime())
    
    if __name__ == "__main__":
        main()
        while True:
            time.sleep(1)

    运行结果:

    Starting at :  Sun Oct 28 13:38:23 2018
    All done at :  Sun Oct 28 13:38:23 2018
    Start loop 1 at :  Sun Oct 28 13:38:23 2018
    Start loop 2 at :  Sun Oct 28 13:38:23 2018
    End loop 2 at :  Sun Oct 28 13:38:25 2018
    End loop 1 at :  Sun Oct 28 13:38:27 2018

    主线程中一直等待,才看看到

    End loop 2 at :  Sun Oct 28 13:38:25 2018
    End loop 1 at :  Sun Oct 28 13:38:27 2018

    否则本例中的主线程执行完比较快,不等待的话, 主线程执行完,不等待loop1和loop2执行网,程序就结束了。

    案例03:多线程,传参数

    '''
    利用time函数,生成两个函数
    利用多线程调用
    计算总的运行时间
    练习带参数的多线程启动方法
    '''
    import time
    #导入多线程包并更名为thread
    import _thread as thread
    def loop1(in1):
        # ctime 得到当前的时间
        print('Start loop 1 at : ',time.ctime())
        #把参数打印出来
        print("我是参数 ",in1)
        #睡眠多长时间 单位是秒
        time.sleep(4)
        print('End loop 1 at : ',time.ctime())
    
    
    def loop2(in1,in2):
        # ctime 得到当前的时间
        print('Start loop 2 at : ',time.ctime())
        #把参数打印出来
        print("我是参数 ",in1," 和参数 ",in2)
        #睡眠多长时间 单位是秒
        time.sleep(2)
        print('End loop 2 at : ',time.ctime())
    
    
    def main():
        print('Starting at : ',time.ctime())
        #启动多线程的意思是用多线程去执行某个函数
        #启动多线程函数为start_new_thread
        #参数两个:一个是需要运行的函数名,第二个是函数的参数作为元组使用,为空则使用空元组
        #注意:如果函数只有一个参数,需要参数后有有一个逗号
        thread.start_new_thread(loop1,("张三",))
        thread.start_new_thread(loop2,("李四","王五"))
    
        print('All done at : ',time.ctime())
    
    if __name__ == "__main__":
        main()
        while True:
            time.sleep(1)

    运行结果:

    Starting at :  Sun Oct 28 13:59:53 2018
    All done at :  Sun Oct 28 13:59:53 2018
    Start loop 1 at :  Sun Oct 28 13:59:53 2018
    Start loop 2 at :  Sun Oct 28 13:59:53 2018
    我是参数  张三
    我是参数  李四  和参数  王五
    End loop 2 at :  Sun Oct 28 13:59:55 2018
    End loop 1 at :  Sun Oct 28 13:59:57 2018

    threading的使用:

      直接利用threading.Thread 生成 Thread 实例

        1. t = threading.Thread(target=xxx,args=(xxx,))  #创建一个多线程实例

        2. t.start()  #启动多线程

        3. t.join()  #等待多线程执行完成

        4. 案例04

  • 相关阅读:
    [转]Java 的强引用、弱引用、软引用、虚引用
    无法向会话状态服务器发出会话状态请求。请确保 ASP.NET State Service (ASP.NET
    [转]理解水平扩展和垂直扩展
    Uva 12299 带循环移动的RMQ(线段树)
    Codeforces 758D Ability To Convert(区间DP)
    Codeforces 758C Unfair Poll
    Codeforces 758B Blown Garland
    Codeforces 758A Holiday Of Equality
    LA 3938 动态最大连续和
    Uva 11235 RMQ问题
  • 原文地址:https://www.cnblogs.com/doitjust/p/9867450.html
Copyright © 2011-2022 走看看