装饰器实现简易限制函数调用频率

#装饰器实现简易限制函数调用频率,如10秒一次
import math
import time
def wrapper(func):
start_time =0
def inner(*args,**kwargs):
nonlocal start_time
t = time.time() - start_time
if t >= 10:
start_time =time.time()
ret = func(*args, **kwargs)
return ret
else:

print(f"技能还在冷却当中,倒计时{math.ceil(10-t)}秒")
return inner


@wrapper
def foo():
print("开始瞄准,射击")
while True:
foo()
time.sleep(1)

猜你喜欢

转载自www.cnblogs.com/niucunguo/p/12038465.html