uopspop

Untitled

Jan 3rd, 2020
77
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import time
  2.  
  3. import redis
  4. from flask import Flask
  5.  
  6. app = Flask(__name__)
  7. cache = redis.Redis(host='redis', port=6379)
  8.  
  9.  
  10. def get_hit_count():
  11.     retries = 5
  12.     while True:
  13.         try:
  14.             return cache.incr('hits')
  15.         except redis.exceptions.ConnectionError as exc:
  16.             if retries == 0:
  17.                 raise exc
  18.             retries -= 1
  19.             time.sleep(0.5)
  20.  
  21.  
  22. @app.route('/')
  23. def hello():
  24.     count = get_hit_count()
  25.     return 'Hello World! I have been seen {} times.\n'.format(count)
RAW Paste Data