


python decorator - a method to limit the number of function calls (call once every 10 seconds)
Let me share with you an article on python decorator - a method to limit the number of function calls (call once every 10 seconds). It has a good reference value and I hope it will be helpful to everyone. Let’s take a look together
This is a blogger’s recent interview question from a large company. Write a decorator and limit the function to be called once every 10 seconds. It was a written test at that time, and I only wrote a rough code. After I came back, I reviewed the basic knowledge of Python decorators and finished writing the code. Decided to write a blog to record it.
Decorators are divided into decorators with parameters and decorators without parameters.
#不带参数的装饰器 @dec1 @dec2 def func(): ... #这个函数声明等价于 func = dec1(dec2(func)) #带参数的装饰器 @dec(some_args) def func(): ... #这个函数声明等价于 func = dec(some_args)(func)
Some details to note about decorators without parameters
1. About decorators The function (decorator) itself
So a decorator generally corresponds to two functions, one is the decorator function, which is used to perform some initialization operations, and the other is decorated_func, which is used to implement the decorated Additional processing for function func. And in order to maintain a reference to func, decorated_func is generally used as an internal function of decorator
def decorator(func): def decorator_func() func() return decorated_func
The decorator function is only called once when the function is declared
The decorator is actually syntactic sugar. It will be called after the function is declared, generate decorated_func, and replace the reference of the func symbol with decorated_func. Every time the func function is called thereafter, decorated_func is actually called (this is very important, after decoration, decorated_func is actually called every time).
>>> def decorator(func): ... def decorated_func(): ... func(1) ... return decorated_func ... #声明时就被调用 >>> @decorator ... def func(x): ... print x ... decorator being called #使用func()函数实际上使用的是decorated_func函数 >>> func() 1 >>> func.__name__ 'decorated_func'
If you want to ensure that the function name of the returned decorated_func is the same as the function name of func, you should add decorated_func.name = func before the decorator function returns decorated_func. name. In addition, the functools module provides the wraps decorator to complete this action.
#@wraps(func)的操作相当于 #在return decorated_func之前,执行 #decorated_func.__name__ = func.__name__ #func作为装饰器参数传入, #decorated_func则作为wraps返回的函数的参数传入 >>> def decorator(func): ... @wraps(func) ... def decorated_func(): ... func(1) ... return decorated_func ... #声明时就被调用 >>> @decorator ... def func(x): ... print x ... decorator being called #使用func()函数实际上使用的是decorated_func函数 >>> func() 1 >>> func.__name__ 'func'
The wonderful use of local variables of the decorator function
Because of the characteristics of closure (see details (1) Detailed explanation of partial closure part), the variables declared by decorator will be referenced by decorated_func.func_closure, so after calling the decorator method, the local variables of the decorator method will not be recycled, so the local variables of the decorator method can be used as Counters, caches, etc.
It is worth noting that if you want to change the value of a variable, the variable must be a mutable object, so even a counter should be implemented with a list. And declare that the function calls the decorator function once, so the counters of different functions do not conflict with each other, for example:
#!/usr/bin/env python #filename decorator.py def decorator(func): #注意这里使用可变对象 a = [0] def decorated_func(*args,**keyargs): func(*args, **keyargs) #因为闭包是浅拷贝,如果是不可变对象,每次调用完成后符号都会被清空,导致错误 a[0] += 1 print "%s have bing called %d times" % (func.__name__, a[0]) return decorated_func @decorator def func(x): print x @decorator def theOtherFunc(x): print x
Let’s start writing the code:
#coding=UTF-8 #!/usr/bin/env python #filename decorator.py import time from functools import wraps def decorator(func): "cache for function result, which is immutable with fixed arguments" print "initial cache for %s" % func.__name__ cache = {} @wraps(func) def decorated_func(*args,**kwargs): # 函数的名称作为key key = func.__name__ result = None #判断是否存在缓存 if key in cache.keys(): (result, updateTime) = cache[key] #过期时间固定为10秒 if time.time() -updateTime < 10: print "limit call 10s", key result = updateTime else : print "cache expired !!! can call " result = None else: print "no cache for ", key #如果过期,或则没有缓存调用方法 if result is None: result = func(*args, **kwargs) cache[key] = (result, time.time()) return result return decorated_func @decorator def func(x): print 'call func'
I just tested it and there is basically no problem.
>>> from decorator import func initial cache for func >>> func(1) no cache for func call func >>> func(1) limit call 10s func 1488082913.239092 >>> func(1) cache expired !!! can call call func >>> func(1) limit call 10s func 1488082923.298204 >>> func(1) cache expired !!! can call call func >>> func(1) limit call 10s func 1488082935.165979 >>> func(1) limit call 10s func 1488082935.165979
Related recommendations:
python limits the number of function calls
The above is the detailed content of python decorator - a method to limit the number of function calls (call once every 10 seconds). For more information, please follow other related articles on the PHP Chinese website!

Create multi-dimensional arrays with NumPy can be achieved through the following steps: 1) Use the numpy.array() function to create an array, such as np.array([[1,2,3],[4,5,6]]) to create a 2D array; 2) Use np.zeros(), np.ones(), np.random.random() and other functions to create an array filled with specific values; 3) Understand the shape and size properties of the array to ensure that the length of the sub-array is consistent and avoid errors; 4) Use the np.reshape() function to change the shape of the array; 5) Pay attention to memory usage to ensure that the code is clear and efficient.

BroadcastinginNumPyisamethodtoperformoperationsonarraysofdifferentshapesbyautomaticallyaligningthem.Itsimplifiescode,enhancesreadability,andboostsperformance.Here'showitworks:1)Smallerarraysarepaddedwithonestomatchdimensions.2)Compatibledimensionsare

ForPythondatastorage,chooselistsforflexibilitywithmixeddatatypes,array.arrayformemory-efficienthomogeneousnumericaldata,andNumPyarraysforadvancednumericalcomputing.Listsareversatilebutlessefficientforlargenumericaldatasets;array.arrayoffersamiddlegro

Pythonlistsarebetterthanarraysformanagingdiversedatatypes.1)Listscanholdelementsofdifferenttypes,2)theyaredynamic,allowingeasyadditionsandremovals,3)theyofferintuitiveoperationslikeslicing,but4)theyarelessmemory-efficientandslowerforlargedatasets.

ToaccesselementsinaPythonarray,useindexing:my_array[2]accessesthethirdelement,returning3.Pythonuseszero-basedindexing.1)Usepositiveandnegativeindexing:my_list[0]forthefirstelement,my_list[-1]forthelast.2)Useslicingforarange:my_list[1:5]extractselemen

Article discusses impossibility of tuple comprehension in Python due to syntax ambiguity. Alternatives like using tuple() with generator expressions are suggested for creating tuples efficiently.(159 characters)

The article explains modules and packages in Python, their differences, and usage. Modules are single files, while packages are directories with an __init__.py file, organizing related modules hierarchically.

Article discusses docstrings in Python, their usage, and benefits. Main issue: importance of docstrings for code documentation and accessibility.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
