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