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

代码

  • 被装饰器标注的函数被调用时会被创建一个线程进行执行
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通过装饰器方式启动多线程)