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

#装饰器实现简易限制函数调用频率,如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)
posted @ 2019-12-14 11:14  未来全栈攻城狮  阅读(667)  评论(0编辑  收藏  举报