Guest User

пример 2

a guest
Jul 22nd, 2013
326
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import psutil
  2. from time import time
  3.  
  4. def timed(fn):
  5.     def decorated(*x):
  6.         start = time()
  7.         result = fn(*x)
  8.         print "Executing %s took %d ms" % (fn.__name__, (time()-start)*1000)
  9.         return result
  10.     return decorated
  11.  
  12. def repeat(times):
  13.     """ повторить вызов times раз, и вернуть среднее значение """
  14.     def decorator(fn):
  15.         def decorated2(*x):
  16.             total = 0
  17.             for i in range(times):
  18.                 total += fn(*x)
  19.             return total / times
  20.         return decorated2
  21.     return decorator
  22.  
  23. @repeat(5)
  24. def cpuload():
  25.     load = psutil.cpu_percent()
  26.     print "cpuload() returns %d" % load
  27.     return load
  28.  
  29. print "cpuload.__name__==" + cpuload.__name__
  30. print "CPU load is %d%%" % cpuload()
RAW Paste Data