app-python3.py 931 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import os
  2. from ujson import dumps as json
  3. from socketify import App
  4. def plaintext(res, req):
  5. res.write_header("Server", "socketify")
  6. res.write_header("Content-Type", "text/plain")
  7. res.end(b'Hello, World!')
  8. def applicationjson(res, req):
  9. res.write_header("Server", "socketify")
  10. res.write_header("Content-Type", "application/json")
  11. res.end(json({"message":"Hello, World!"}))
  12. def run_app():
  13. app = app = App(None, 200_000, 0)
  14. app.get("/", plaintext)
  15. app.get("/json", applicationjson)
  16. app.get("/plaintext", plaintext)
  17. app.listen(3000)
  18. app.run()
  19. def create_fork():
  20. n = os.fork()
  21. # n greater than 0 means parent process
  22. if not n > 0:
  23. run_app()
  24. def get_worker_count():
  25. try:
  26. return int(os.environ["WORKER_COUNT"])
  27. except:
  28. return 2
  29. WORKER_COUNT = get_worker_count() - 1
  30. for index in range(WORKER_COUNT):
  31. create_fork()
  32. run_app()