1.异步无参装饰器
import time
from functools import wraps
def total_time(func):
@wraps(func)
async def wrapper(*args, **kwargs):
start = time.time()
res = await func(*args, **kwargs)
print(f'RUN TIME: {time.time() - start}')
return res
return wrapper
2.异步有参数装饰器
import time
from functools import wraps
def set_num(desc: str):
def outer(func):
@wraps(func)
async def wrapper(*args, **kwargs):
start = time.time()
res = await func(*args, **kwargs)
print(f'RUN TIME - {desc}: {time.time() - start}')
return res
return wrapper
return outer
标签:wraps,return,res,func,time,async,装饰,def
From: https://www.cnblogs.com/lxd670/p/17603620.html