gunicorn_config.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # -*- coding:utf-8 -*-
  2. """
  3. gunicorn 配置项
  4. """
  5. # Gunicorn的配置可以参考:
  6. # https://blog.csdn.net/y472360651/article/details/78538188
  7. # https://docs.gunicorn.org/en/stable/settings.html#server-mechanics
  8. """
  9. if __name__ == '__main__':
  10. uvicorn.run(app='main:app', host="127.0.0.1", port=8000, reload=True, debug=True)
  11. requirements:
  12. httptooles, uvloop, gunicorn, uvicorn
  13. shell:
  14. uvicorn:
  15. uvicorn local:app --reload --port 5000
  16. gunicorn:
  17. gunicorn api_main:app -b 0.0.0.0:8001 -w 4 -k uvicorn.workers.UvicornWorker # -D 守护启动 -c 配置文件
  18. """
  19. import os
  20. from config import CfgBaseInit
  21. project_path = CfgBaseInit.project_path
  22. project_name = CfgBaseInit.project_name
  23. # debug 开启
  24. debug = True
  25. # 后台守护模式
  26. # daemon = True
  27. daemon = False # 如果考虑使用 supervisor 的话,将此关闭较为稳妥
  28. # 更改代码后台重新加载
  29. # reload = True
  30. reload = False
  31. # 预加载资源
  32. preload_app = True
  33. bind = CfgBaseInit.bind # 绑定ip和端口号
  34. # backlog = 512 # 监听队列
  35. chdir = '{}'.format(project_path) # gunicorn要切换到的目的工作目录
  36. timeout = 180 # 超时
  37. # worker_class = 'gevent' #使用gevent模式,还可以使用sync 模式,默认的是sync模式
  38. worker_class = 'uvicorn.workers.UvicornWorker' # sync 模式, fastapi 框架使用的是 asgi ,wsgi 可使用 gevent 模式
  39. # 设置最大并发量
  40. worker_connections = 10000
  41. # 长链接保持时间 1-5s, 默认 2
  42. keepalive = 180
  43. # workers = multiprocessing.cpu_count() * 2 + 1 # 进程数
  44. workers = 3 # 进程数
  45. threads = min(32, (os.cpu_count() or 1) + 4) # if "uvicorn" not in worker_class else 1 # 指定每个进程开启的线程数
  46. loglevel = 'debug' # 日志级别,这个日志级别指的是错误日志的级别,而访问日志的级别无法设置
  47. access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"' # 设置gunicorn访问日志格式,错误日志无法设置
  48. """
  49. 其每个选项的含义如下:
  50. h remote address
  51. l '-'
  52. u currently '-', may be user name in future releases
  53. t date of the request
  54. r status line (e.g. ``GET / HTTP/1.1``)
  55. s status
  56. b response length or '-'
  57. f referer
  58. a user agent
  59. T request time in seconds
  60. D request time in microseconds
  61. L request time in decimal seconds
  62. p process ID
  63. """
  64. # 如果使用service启动,这里的pid路径应与service的pid路径保持一致,否则无法启动
  65. # pidfile = "./log/fast.pid"
  66. pidfile = "{}/../logs/{}/pid.pid".format(project_path, project_name)
  67. accesslog = "{}/../logs/{}/gunicorn_access.log".format(project_path, project_name) # 访问日志文件
  68. errorlog = "{}/../logs/{}/gunicorn_error.log".format(project_path, project_name) # 错误日志文件