python裝飾器-限制函數調用次數的方法(10s調用一次)
這是博主最近一家大公司的面試題,寫一個裝飾器,限制函數每10s調用一次。當時是筆試的,只寫了大概的代碼,回來后溫習了python裝飾器的基礎知識,把代碼寫完了。決定寫篇博客記錄下。
裝飾器分為帶參數得裝飾器以及不帶參數得裝飾器。
#不帶參數的裝飾器 @dec1 @dec2 def func(): ... #這個函數聲明等價于 func = dec1(dec2(func)) #帶參數的裝飾器 @dec(some_args) def func(): ... #這個函數聲明等價于 func = dec(some_args)(func)
不帶參數的裝飾器需要注意的一些細節(jié)
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之前,執(zhí)行 #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 'call func'
隨便測試了下,基本沒有問題。
>>> 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裝飾器-限制函數調用次數的方法(10s調用一次)就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Flask框架URL管理操作示例【基于@app.route】
這篇文章主要介紹了Flask框架URL管理操作,結合實例形式分析了@app.route進行URL控制的相關操作技巧,需要的朋友可以參考下2018-07-07

