回答

0

呼籲func每10萬個電話,與@counter使用您函數之前:

def counter(func): 
    def wrapper(val, *args, **kwargs): 
     if val: 
      wrapper.count = wrapper.count + 1 
      if wrapper.count % 100000 == 0: # insert 100,000 at a time, should be speedier thanks to `.executemany()` 
       to_logfile(str(wrapper.count) + '\n', 'counter2.log') 
       return func(wrapper.valarr, *args, **kwargs) 
      else: 
       if len(wrapper.valarr)==1000000: 
        wrapper.vallarr = [] 
       wrapper.valarr.append([val]) 
       return 
    wrapper.count = 0 
    wrapper.valarr = [] 

    return wrapper 

(隨時提出改進)

0

這裏有一個類實現,正如其他人所提交的功能implentations:

class IntervalCache(object): 

    def __init__(self, f): 
     self._f = f 
     self._cache = [] 
     self._n = 0 
     self._interval = 1000 

    def __call__(self, *args, **kwargs): 
     if self._n == self._interval: 
      result = [self.f(*c_args, **c_kwargs) for c_args, c_kwargs in self._cache] 
      self._n = 0 
      self._cache = [] 
      return result + [self.f(*args, **kwargs)] 
     else: 
      self._cache.append((*args, **kwargs)) 
      self._n += 1 

    @property 
    def getCache(self): 
     return self._cache 

    def resetCache(self): 
     self._cache = [] 
     self._n = 0 

    def getInterval(self): 
     return self._interval 

    def setInterval(self, value): 
     self._interval = value 

    interval = property(getInterval, setInterval) 

用法:

#wrapping a function 

@IntervalCache 
def foo(*args, **kwargs): 
    print args, kwargs 
    return True 

#setting the caching interval 
foo.interval = 10 

#calling foo a bunch of times 
for i in range(20): 
    print foo(i, i+1, bar=i*2) 

#retrieving the contents of the cache 
print foo.getCache() 

#resetting the contents of the cache 
foo.resetCache() 
0

我要假定

  • 你的函數接受任何數目的位置參數
  • f(a); f(b)相同f(a, b)
  • 它沒有返回值(任何結果的發生是由於副作用)

import functools 

class Batcher(object): 
    def __init__(self, n=100): 
     self.n = n 

    def __call__(self, fn): 
     @functools.wraps(fn) 
     def _fn(*args): 
      _fn.cache.extend(args) 
      _fn.calls += 1 
      if _fn.calls == _fn.n: 
       fn(*_fn.cache) 
       _fn.cache = [] 
       _fn.calls = 0 
     _fn.n = self.n 
     _fn.cache = [] 
     _fn.calls = 0 
     return _fn 

然後測試它,

@Batcher(20) 
def mysum(*args): 
    print sum(args) 

for i in range(1,25): 
    mysum(i) 

打印

210 # <- result of sum(1..20) 
相關問題