app-socketify-wsgi.py 1012 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env python
  2. from flask import Flask, make_response, jsonify
  3. import os
  4. import multiprocessing
  5. import logging
  6. from socketify import WSGI
  7. # setup
  8. app = Flask(__name__)
  9. app.config["JSONIFY_PRETTYPRINT_REGULAR"] = False
  10. @app.route("/json")
  11. def hello():
  12. return jsonify(message="Hello, World!")
  13. @app.route("/plaintext")
  14. def plaintext():
  15. """Test 6: Plaintext"""
  16. response = make_response(b"Hello, World!")
  17. response.content_type = "text/plain"
  18. return response
  19. _is_travis = os.environ.get('TRAVIS') == 'true'
  20. workers = int(multiprocessing.cpu_count())
  21. if _is_travis:
  22. workers = 2
  23. def run_app():
  24. WSGI(app).listen(8080, lambda config: logging.info(f"Listening on http://localhost:{config.port} now\n")).run()
  25. def create_fork():
  26. n = os.fork()
  27. # n greater than 0 means parent process
  28. if not n > 0:
  29. run_app()
  30. # fork limiting the cpu count - 1
  31. for i in range(1, multiprocessing.cpu_count()):
  32. create_fork()
  33. run_app() # run app on the main process too :)