欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > python >内容正文

python

python调用函数指定次数_扣丁学堂Python在线视频之Python限制函数调用次数实例

发布时间:2024/1/1 python 42 豆豆
生活随笔 收集整理的这篇文章主要介绍了 python调用函数指定次数_扣丁学堂Python在线视频之Python限制函数调用次数实例 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

扣丁学堂Python在线视频之Python限制函数调用次数实例

2018-07-06 14:56:43

859浏览

本篇文章扣丁学堂

博客中指定的是缓存,我这里换成限制访问次数,异曲同工:

#newtest.py

#!/usr/bin/env python

#-*- coding:utf-8 -*-

import time

def stat_called_time(func):

cache={}

limit_times=[10]

def _called_time(*args,**kwargs):

key=func.__name__

if key in cache.keys():

[call_times,updatetime]=cache[key]

if time.time()-updatetime <60:

cache[key][0]+=1

else:

cache[key]=[1,time.time()]

else:

call_times=1

cache[key]=[call_times,time.time()]

print('调用次数: %s' % cache[key][0])

print('限制次数: %s' % limit_times[0])

if cache[key][0] <= limit_times[0]:

res=func(*args,**kwargs)

cache[key][1] = time.time()

return res

else:

print("超过调用次数了")

return None

return _called_time

@stat_called_time

def foo():

print("I'm foo")

if __name__=='__main__':

for i in range(10):

foo()

#test.py

from newtest import foo

import time

for i in range(30):

foo()

print('*'*20)

foo()

foo()

print('*'*20)

for i in range(60):

print(i)

time.sleep(1)

for i in range(11):

foo()

以上就是扣丁学堂

【关注微信公众号获取更多学习资料】

标签:

扣丁学堂Python在线视频

Python限制函数调用次数实例

Python培训

Python视频教程

Python基础教程

python安装教程

Python核心编程

Python在线教程

Python在线视频

Python在线学习

总结

以上是生活随笔为你收集整理的python调用函数指定次数_扣丁学堂Python在线视频之Python限制函数调用次数实例的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。