python 异步执行装饰器

使用多线程写一个简单的异步调用装饰器:

async_call.py:

import threading
from conf.log_handlers import log_task

lock = threading.Lock()

def async(f):
    def wrapper(*args, **kwargs):
        thr = threading.Thread(target = f, args = args, kwargs = kwargs)
        thr.start()
        thr.setName("方法{}".format(f.__name__))
        # thr.join()
        log.info("线程id={},\n线程名称={},\n正在执行的线程列表:{},\n正在执行的线程数量={},\n当前激活线程={}".format(
            thr.ident,thr.getName(),threading.enumerate(),threading.active_count(),thr.isAlive)
        )
    return wrapper

test.py

@async
def test():
    time.sleep(5)

你可能感兴趣的:(python 异步执行装饰器)