Advertisement
extrn

Untitled

Nov 3rd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.12 KB | None | 0 0
  1. import time
  2. import random
  3.  
  4. def cache(timeout):
  5.     def wrap1(fn):
  6.         last_access = None
  7.         cached_result = None
  8.         def wrap2():
  9.             nonlocal last_access, cached_result
  10.              
  11.             now = time.time()
  12.             if not last_access or abs(last_access - now) > timeout:
  13.                 cached_result = fn()
  14.                 last_access = now
  15.             return cached_result
  16.         return wrap2
  17.     return wrap1                
  18.  
  19. @cache(0.5)
  20. def param1():
  21.     return random.randint(0, 100000)
  22.    
  23. @cache(2)
  24. def param2():
  25.     return hex(random.randint(0, 10**10))
  26.  
  27. @cache(5)
  28. def param3():
  29.     return [random.randint(0, 255) for _ in range(10)]
  30.  
  31. def application(environ, start_response):
  32.     start_response('200 OK', [('Content-Type', 'text/plain')])
  33.  
  34.     out = []
  35.     out.append('param1: ' + str(param1()))
  36.     out.append('param2: ' + str(param2()))
  37.     out.append('param3: ' + str(param3()))
  38.    
  39.     return ["\n".join(out).encode()]    
  40.    
  41. if __name__ == '__main__':
  42.     from wsgiref.simple_server import make_server
  43.     make_server('', 80, application).serve_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement