app-fastwsgi.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/usr/bin/env python
  2. from flask import Flask, make_response, jsonify
  3. import os
  4. import multiprocessing
  5. import logging
  6. import fastwsgi
  7. # setup
  8. app = Flask(__name__)
  9. app.config["JSONIFY_PRETTYPRINT_REGULAR"] = False
  10. @app.route("/json")
  11. def hello():
  12. response = make_response(jsonify(message="Hello, World!"))
  13. response.content_type = "application/json"
  14. response.headers.set('Server', 'FastWSGI+Flask')
  15. return response
  16. @app.route("/plaintext")
  17. def plaintext():
  18. response = make_response(b"Hello, World!")
  19. response.content_type = "text/plain"
  20. response.headers.set('Server', 'FastWSGI+Flask')
  21. return response
  22. _is_travis = os.environ.get('TRAVIS') == 'true'
  23. workers = int(multiprocessing.cpu_count())
  24. if _is_travis:
  25. workers = 2
  26. host = '0.0.0.0'
  27. port = 8080
  28. def run_app():
  29. fastwsgi.run(app, host, port, loglevel=0)
  30. def create_fork():
  31. n = os.fork()
  32. # n greater than 0 means parent process
  33. if not n > 0:
  34. run_app()
  35. # fork limiting the cpu count - 1
  36. for i in range(1, workers):
  37. create_fork()
  38. run_app() # run app on the main process too :)