zoukankan      html  css  js  c++  java
  • 【Rollo的Python之路】多线程实例 Join 与Daemon

    1.0 threading 的join方法就是用来阻塞用的,可以阻塞主线程,等待所的子线程结束后,然后再运行后面的程序:

    import threading
    import time
    from time import ctime,sleep
    
    def music(func):
        for i in range(2):
            print("I was listening to %s.%s" %(func,ctime()))
            sleep(1)
            print("end listening %s" % ctime())
    
    def movie(func):
        for i in range(2):
            print("I was at the %s %s" %(func,ctime()))
            sleep(5)
            print("end watch %s" % ctime())
    
    threads = []
    
    t1 = threading.Thread(target=music,args=("overstock",))
    threads.append(t1)
    t2 = threading.Thread(target=movie,args=("kill them all",))
    threads.append(t2)
    
    if __name__ =="__main__":
        for t in threads:
            t.start()
    
        t2.join()
        print("all over %s" %ctime())

    此时,会等t2运行完成后,才执行最后的print语句。

    import threading
    import time
    from time import ctime,sleep
    
    def music(func):
        for i in range(2):
            print("I was listening to %s.%s" %(func,ctime()))
            sleep(1)
            print("end listening %s" % ctime())
    
    def movie(func):
        for i in range(2):
            print("I was at the %s %s" %(func,ctime()))
            sleep(5)
            print("end watch %s" % ctime())
    
    threads = []
    
    t1 = threading.Thread(target=music,args=("overstock",))
    threads.append(t1)
    t2 = threading.Thread(target=movie,args=("kill them all",))
    threads.append(t2)
    
    if __name__ =="__main__":
        for t in threads:
            t.start()
    
        t1.join()
        print("all over %s" %ctime())

    此时,只会运行完t1后,就会执行最后的print

    2.0 threading 的daemon,当我们使用setDaemon(True)方法,设置子线程为守护线程时,主线程一旦执行结束,则全部线程全部被终止执行,可能出现的情况就是,子线程的任务还没有完全执行结束,就被迫停止

    import threading
    import time
    from time import ctime,sleep
    
    def music(func):
        for i in range(2):
            print("I was listening to %s.%s" %(func,ctime()))
            sleep(1)
            print("end listening %s" % ctime())
    
    def movie(func):
        for i in range(2):
            print("I was at the %s %s" %(func,ctime()))
            sleep(5)
            print("end watch %s" % ctime())
    
    threads = []
    
    t1 = threading.Thread(target=music,args=("overstock",))
    threads.append(t1)
    t2 = threading.Thread(target=movie,args=("kill them all",))
    threads.append(t2)
    
    if __name__ =="__main__":
        for t in threads:
            t.setDaemon(True)
            t.start()
    
        print("all over %s" %ctime())

    跑完主线程,直接关闭程序。

    import threading
    import time
    from time import ctime,sleep
    
    def music(func):
        for i in range(2):
            print("I was listening to %s.%s" %(func,ctime()))
            sleep(1)
            print("end listening %s" % ctime())
    
    def movie(func):
        for i in range(2):
            print("I was at the %s %s" %(func,ctime()))
            sleep(5)
            print("end watch %s" % ctime())
    
    threads = []
    
    t1 = threading.Thread(target=music,args=("overstock",))
    threads.append(t1)
    t2 = threading.Thread(target=movie,args=("kill them all",))
    threads.append(t2)
    
    if __name__ =="__main__":
        t2.setDaemon(True)
        for t in threads:
            
            t.start()
    
        print("all over %s" %ctime())

    t2 setDaemon(True),守护线程,t1还是得等,但是只要t1运行完,就会不等t2,直接关闭程序

    3.0 threading.current_thread()  线程名

    4.0 threading.active_count() 在运行的线程数量。

  • 相关阅读:
    Java遍历JsonObject对象
    fastjson.JSONObject之对象与JSON转换方法
    Java HotSpot VM中的JIT编译
    JAVA 反射类 捕获异常 method.invoke方法如何捕获异常
    手动调用hibernate的参数校验器和springboot参数校验器 验证
    Netty-Socketio API
    Netty-socketio集成redis,服务端集群推送消息
    mysql 导出csv格式数据解决乱码
    自建dns服务器
    MySQL 8.0 克隆(clone)插件快速搭建主从复制
  • 原文地址:https://www.cnblogs.com/rollost/p/10914890.html
Copyright © 2011-2022 走看看