美文网首页
Python-学习之路-15 多线程1

Python-学习之路-15 多线程1

作者: 末世狂人 | 来源:发表于2019-03-03 23:16 被阅读0次

    多线程

    全局解释器(GIL)

    • Python 代码的执行由Python虚拟机进行控制
    • 在主循环中只能有一个控制线程在执行

    Python包

    • thread包存在问题,在python3中改成了_thread
    • threading:通行的包
      • threading的使用
        • 直接使用threading.Thread生成Thread
        1. t = threading.Thread(target=xxx, args=(xxx,))
        2. t.start();启动线程
        3. t.join();等待线程完成
    • 守护线程 daemon
      • 如果在程序子线程设置成守护线程,则子线程会在主线程结束的时候自动结束
      • 一般认为,守护线程不重要或者不允许离开主线程独立运行
      • 守护线程案例能否有效果跟环境有关
    # 多线程 案例1
    import time
    import _thread as thread
    
    def loop1():
        # ctime 获取当前时间
        print("start loop 1 at :",time.ctime())
        
        #休眠4秒
        time.sleep(4)
        print("end loop 1 at :",time.ctime())
        
    def loop2():
        # ctime 获取当前时间
        print("start loop 2 at :",time.ctime())
        
        #休眠4秒
        time.sleep(2)
        print("end loop 2 at :",time.ctime())
        
    def main():
        print("start at :",time.ctime())
        thread.start_new_thread(loop1,())
        thread.start_new_thread(loop2,())
        print("all done at :",time.ctime())
        
    if __name__ == '__main__':
        main()
    
    start at : Sun Mar  3 22:41:12 2019
    all done at : Sun Mar  3 22:41:12 2019
    start loop 2 at : Sun Mar  3 22:41:12 2019
    start loop 1 at : Sun Mar  3 22:41:12 2019
    end loop 2 at : Sun Mar  3 22:41:14 2019
    end loop 1 at : Sun Mar  3 22:41:16 2019
    
    # 多线程 案例2
    import time
    import _thread as thread
    
    def loop1(int1):
        # ctime 获取当前时间
        print("start loop 1 at :",time.ctime())
        print("我是参数:",int1)
        #休眠4秒
        time.sleep(4)
        print("end loop 1 at :",time.ctime())
        
    def loop2(int1, int2):
        # ctime 获取当前时间
        print("start loop 2 at :",time.ctime())
        print("我是参数1:{0},我是参数2:{1}".format(int1,int2))
        #休眠4秒
        time.sleep(2)
        print("end loop 2 at :",time.ctime())
        
    def main():
        print("start at :",time.ctime())
        thread.start_new_thread(loop1,("王老大",))
        thread.start_new_thread(loop2,("张三","李四"))
        print("all done at :",time.ctime())
        
    if __name__ == '__main__':
        main()
    
    start at : Sun Mar  3 22:43:48 2019
    all done at : Sun Mar  3 22:43:48 2019
    start loop 2 at : Sun Mar  3 22:43:48 2019
    我是参数1:张三,我是参数2:李四
    start loop 1 at : Sun Mar  3 22:43:48 2019
    我是参数: 王老大
    end loop 2 at : Sun Mar  3 22:43:50 2019
    end loop 1 at : Sun Mar  3 22:43:52 2019
    
    # 多线程 案例3
    import time
    import threading
    
    def loop1(int1):
        # ctime 获取当前时间
        print("start loop 1 at :",time.ctime())
        print("我是参数:",int1)
        #休眠4秒
        time.sleep(4)
        print("end loop 1 at :",time.ctime())
        
    def loop2(int1, int2):
        # ctime 获取当前时间
        print("start loop 2 at :",time.ctime())
        print("我是参数1:{0},我是参数2:{1}".format(int1,int2))
        #休眠4秒
        time.sleep(2)
        print("end loop 2 at :",time.ctime())
        
    def main():
        print("start at :",time.ctime())
        t1 = threading.Thread(target=loop1, args=("王小虎",))
        t2 = threading.Thread(target=loop2, args=("熊大","熊二"))
        t1.start()
        t2.start()
        
        t1.join()
        t2.join()
        print("all done at :",time.ctime())
        
    if __name__ == '__main__':
        main()
    
    start at : Sun Mar  3 22:53:12 2019
    start loop 1 at : Sun Mar  3 22:53:12 2019
    我是参数: 王小虎
    start loop 2 at : Sun Mar  3 22:53:12 2019
    我是参数1:熊大,我是参数2:熊二
    end loop 2 at : Sun Mar  3 22:53:14 2019
    end loop 1 at : Sun Mar  3 22:53:16 2019
    all done at : Sun Mar  3 22:53:16 2019
    
    # 守护线程 案例1
    import time
    import threading
    
    def fun():
        print("start fun")
        time.sleep(2)
        print("end fun")
        
    print("Main start")
    t = threading.Thread(target= fun, args=())
    t.setDaemon(True)
    t.start()
    time.sleep(1)
    print("Main end")
    
    Main start
    start fun
    Main end
    end fun
    

    线程的当用属性

    • threading.currentThread: 返回当前线程变量
    • threading.activeCount: 返回正在运行的线程数量
    • threading.enumerate: 返回一个包含正在运行的线程的list
    • threading.setName:给线程设置名字
    • threading.getName:返回线程的名字

    直接继承自threading.Thread

    • 直接继承Thread
    • 重写run方法
    • 类实例可以直接运行

    相关文章

      网友评论

          本文标题:Python-学习之路-15 多线程1

          本文链接:https://www.haomeiwen.com/subject/lmevuqtx.html