uopspop

Untitled

Aug 15th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. import time
  2.  
  3. import redis
  4. from flask import Flask
  5.  
  6.  
  7. app = Flask(__name__)
  8. cache = redis.Redis(host='redis', port=6379)
  9.  
  10.  
  11. def get_hit_count():
  12. retries = 5
  13. while True:
  14. try:
  15. return cache.incr('hits')
  16. except redis.exceptions.ConnectionError as exc:
  17. if retries == 0:
  18. raise exc
  19. retries -= 1
  20. time.sleep(0.5)
  21.  
  22.  
  23. @app.route('/')
  24. def hello():
  25. count = get_hit_count()
  26. return 'Hello World! I have been seen {} times.\n'.format(count)
  27.  
  28. if __name__ == "__main__":
  29. app.run(host="0.0.0.0", debug=True)
Add Comment
Please, Sign In to add comment