gunicorn_conf.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # based on https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker
  2. import multiprocessing
  3. import os
  4. host = os.getenv("HOST", "0.0.0.0")
  5. port = os.getenv("PORT", "8080")
  6. bind_env = os.getenv("BIND", None)
  7. use_bind = bind_env if bind_env else f"{host}:{port}"
  8. workers_per_core_str = os.getenv("WORKERS_PER_CORE", "1")
  9. max_workers_str = os.getenv("MAX_WORKERS")
  10. web_concurrency_str = os.getenv("WEB_CONCURRENCY", None)
  11. cores = multiprocessing.cpu_count()
  12. workers_per_core = int(workers_per_core_str)
  13. default_web_concurrency = workers_per_core * cores + 1
  14. if web_concurrency_str:
  15. web_concurrency = int(web_concurrency_str)
  16. assert web_concurrency > 0
  17. else:
  18. web_concurrency = max(int(default_web_concurrency), 2)
  19. if max_workers_str:
  20. use_max_workers = int(max_workers_str)
  21. web_concurrency = min(web_concurrency, use_max_workers)
  22. graceful_timeout_str = os.getenv("GRACEFUL_TIMEOUT", "120")
  23. timeout_str = os.getenv("TIMEOUT", "120")
  24. keepalive_str = os.getenv("KEEP_ALIVE", "5")
  25. use_loglevel = os.getenv("LOG_LEVEL", "info")
  26. # Gunicorn config variables
  27. loglevel = use_loglevel
  28. workers = web_concurrency
  29. bind = use_bind
  30. worker_tmp_dir = "/dev/shm"
  31. graceful_timeout = int(graceful_timeout_str)
  32. timeout = int(timeout_str)
  33. keepalive = int(keepalive_str)
  34. # logconfig = os.getenv("LOG_CONFIG", "/src/logging_production.ini")