python decorator - a method to limit the number of function calls (call once every 10 seconds)

不言
Release: 2018-04-21 14:34:59
Original
2359 people have browsed it

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)
Copy after login

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
Copy after login

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'
Copy after login

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'
Copy after login

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
Copy after login

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 &#39;call func&#39;
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!