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

  • 相关阅读:
    【服务总线 Azure Service Bus】ServiceBus 队列中死信(DLQ
    【API管理 APIM】APIM集成内部VNet时,常遇见的关于自定义DNS服务问题。
    【Azure云服务 Cloud Service】Cloud Service的实例(VM)中的服务描述Software Protection 与 Windows Defender, 如何设置Windows Defender Antivirus服务
    【事件中心 Azure Event Hub】关于EventHub中出现Error时候的一些问题(偶发错误,EventHub后台升级,用户端错误,Retry机制的重要性)
    【机器学习 Azure Machine Learning】使用VS Code登录到Linux VM上 (Remote-SSH), 及可直接通过VS Code编辑VM中的文件
    【机器学习 Azure Machine Learning】使用Aure虚拟机搭建Jupyter notebook环境,为Machine Learning做准备(Ubuntu 18.04,Linux)
    【API管理 APIM】APIM中对后端API服务的DNS域名缓存问题
    【机器学习 Azure Machine Learning】Azure Machine Learning 访问SQL Server 无法写入问题 (使用微软Python AML Core SDK)
    3、hive存储格式
    玄学搜索随稽化
  • 原文地址:https://www.cnblogs.com/doitjust/p/9867450.html
Copyright © 2011-2022 走看看