Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import random
- def cache(timeout):
- def wrap1(fn):
- last_access = None
- cached_result = None
- def wrap2():
- nonlocal last_access, cached_result
- now = time.time()
- if not last_access or abs(last_access - now) > timeout:
- cached_result = fn()
- last_access = now
- return cached_result
- return wrap2
- return wrap1
- @cache(0.5)
- def param1():
- return random.randint(0, 100000)
- @cache(2)
- def param2():
- return hex(random.randint(0, 10**10))
- @cache(5)
- def param3():
- return [random.randint(0, 255) for _ in range(10)]
- def application(environ, start_response):
- start_response('200 OK', [('Content-Type', 'text/plain')])
- out = []
- out.append('param1: ' + str(param1()))
- out.append('param2: ' + str(param2()))
- out.append('param3: ' + str(param3()))
- return ["\n".join(out).encode()]
- if __name__ == '__main__':
- from wsgiref.simple_server import make_server
- make_server('', 80, application).serve_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement