首頁  >  文章  >  後端開發  >  python裝飾器-限制函數呼叫次數的方法(10s呼叫一次)

python裝飾器-限制函數呼叫次數的方法(10s呼叫一次)

不言
不言原創
2018-04-21 14:34:592358瀏覽

下面為大家分享一篇python裝飾器-限制函數呼叫次數的方法(10s呼叫一次),具有很好的參考價值,希望對大家有幫助。一起來看看吧

這是部落客最近一家大公司的面試題,寫一個裝飾器,限制函數每10s調用一次。當時是筆試的,只寫了大概的程式碼,回來後溫習了python裝飾器的基礎知識,把程式碼寫完了。決定寫篇部落格記錄下。

裝飾器分為帶參數得裝飾器以及不帶參數得裝飾器。

#不带参数的装饰器
@dec1
@dec2
def func():
  ...
#这个函数声明等价于
func = dec1(dec2(func))
#带参数的装饰器
@dec(some_args)
def func():
  ...
#这个函数声明等价于
func = dec(some_args)(func)

不帶參數的裝飾器需要注意的一些細節

1. 關於裝飾器函數(decorator)本身

因此一個裝飾器一般對應兩個函數,一個是decorator函數,用來進行一些初始化操作處理,一個是decorated_func用來實現對被裝飾的函數func的額外處理。並且為了保持對func的引用,decorated_func一般作為decorator的內部函數

#
def decorator(func):
  def decorator_func()
    func()
  return decorated_func

decorator函數只在函數宣告的時候被呼叫一次

裝飾器實際上是語法糖,在宣告函數之後就會被調用,產生decorated_func,並把func符號的引用替換為decorated_func。之後每次都呼叫func函數,實際呼叫的是decorated_func(這個很重要,裝飾之後,其實每次呼叫的是decorated_func)。

>>> 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'

如果要保證傳回的decorated_func的函數名稱與func的函數名稱相同,應在decorator函數傳回decorated_func之前,加入decorated_func.name = func. name, 另外functools模組提供了wraps裝飾器,可以完成這一動作。

#@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'

decorator函數局部變數的妙用

因為closure的特性(詳見(1)部分閉包部分的詳解),decorator宣告的變數會被decorated_func.func_closure引用,所以呼叫了decorator方法結束之後,decorator方法的局部變數也不會被回收,因此可以用decorator方法的局部變數作為計數器,緩存等等。

值得注意的是,如果要改變變數的值,則變數一定要是可變對象,因此就算是計數器,也應當用列表來實現。且宣告一次函數呼叫一次decorator函數,所以不同函數的計數器之間互不衝突,例如:

#
#!/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

下面我們開始寫程式碼:

#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;

隨便測試了下,基本上沒有問題。

>>> 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

相關推薦:

python 限制函數呼叫次數

########

以上是python裝飾器-限制函數呼叫次數的方法(10s呼叫一次)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn