美文网首页
python通过装饰器方式启动多线程

python通过装饰器方式启动多线程

作者: Time一柒 | 来源:发表于2022-11-02 14:27 被阅读0次

    代码

    • 被装饰器标注的函数被调用时会被创建一个线程进行执行
    from time import sleep
    from threading import Thread
    
    def async_thread(function):
        def wrapper(*args, **kwargs):
            thr = Thread(target=function, args=args, kwargs=kwargs)
            thr.start()
        return wrapper
    
    @async_thread
    def A(a):
        sleep(a)
        print("3秒钟。。。。。。")
        print("a 结束")
    
    def B():
        print("b 结束")
    
    
    A(3)
    B()
    

    相关文章

      网友评论

          本文标题:python通过装饰器方式启动多线程

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