gunicorn.conf.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # gunicorn.conf.py
  2. import os
  3. import multiprocessing
  4. import logging.handlers
  5. import logging
  6. try:
  7. import gevent.monkey
  8. gevent.monkey.patch_all()
  9. except ImportError:
  10. pass
  11. bind = '127.0.0.1:5001'
  12. timeout = 30 # 超时
  13. worker_class = 'gevent'
  14. workers = multiprocessing.cpu_count() * 2 + 1 # 进程数
  15. threads = 2 # 指定每个进程开启的线程数
  16. henglish_path = os.path.join(os.environ['HOME'], "henglish")
  17. log_path = os.path.join(henglish_path, "log")
  18. os.makedirs(log_path, exist_ok=True, mode=0o775)
  19. # 设置访问日志和错误信息日志路径
  20. log_format = ("%(levelname)s:%(name)s:%(asctime)s "
  21. "(%(filename)s:%(lineno)d %(funcName)s) "
  22. "%(process)d %(thread)d "
  23. "%(message)s")
  24. log_formatter = logging.Formatter(log_format)
  25. # 错误日志
  26. gunicorn_error_logger = logging.getLogger("gunicorn.error")
  27. gunicorn_error_logger.setLevel(logging.WARNING)
  28. errorlog = os.path.join(log_path, "gunicorn_error.log")
  29. time_handle = logging.handlers.TimedRotatingFileHandler(errorlog, when="d", backupCount=30, encoding='utf-8')
  30. gunicorn_error_logger.addHandler(time_handle)
  31. time_handle.setFormatter(log_formatter)
  32. # 一般日志
  33. gunicorn_access_logger = logging.getLogger("gunicorn.access")
  34. gunicorn_access_logger.setLevel(logging.INFO)
  35. accesslog = os.path.join(log_path, "gunicorn_access.log")
  36. time_handle = logging.handlers.TimedRotatingFileHandler(accesslog, when="d", backupCount=10, encoding='utf-8')
  37. gunicorn_access_logger.addHandler(time_handle)
  38. time_handle.setFormatter(log_formatter)
  39. # 输出日志
  40. gunicorn_access_logger.info("Load gunicorn conf success")
  41. gunicorn_access_logger.info(f"bind: {bind}")
  42. gunicorn_access_logger.info(f"timeout: {timeout}")
  43. gunicorn_access_logger.info(f"worker_class: {worker_class}")
  44. gunicorn_access_logger.info(f"workers: {workers}")
  45. gunicorn_access_logger.info(f"threads: {threads}")
  46. gunicorn_access_logger.info(f"henglish_path: {henglish_path}")