gunicorn安装及基本配置,对应fastapi框架
gunicorn是python版本的web服务器,也是fastapi框架官方推荐的服务器,工作中使用gunicorn时有一些基本配置需要注意,具体可以参考如下。
如果想查最完整的配置,请参考官方配置文档:https://docs.gunicorn.org/en/stable/settings.html
本文档服务器为centos7,python环境为python3.8,框架使用了fastapi框架。1. 安装gunicorn
conda install gunicorn
2. 配置gunicorn
通常放入项目目录下的gunicorn.conf.py文件。具体配置如下:
# 监听ip和端口 bind = '0.0.0.0:8379' # 工作目录 chdir = '/testdir/testproject' # 工作进程数 workers = 4 # 每个进程对应的工作线程数量,只对gthread起作用 threads = 4 # 待处理连接数量 backlog = 512 # 进程休眠后超过该时间(秒)就会被关闭或者重启 timeout = 120 # 设置守护进程 daemon = True # 工作模式协程 worker_class = 'uvicorn.workers.UvicornWorker' # 设置每个进程的连接量 worker_connections = 1024 # 设置进程文件目录 pidfile = '/testdir/testproject/log_files/testproject.pid' # 设置访问日志和错误信息日志路径 accesslog = '/testdir/testproject/log_files/testproject_access.log' errorlog = '/testdir/testproject/log_files/testproject_error.log' # 访问日志格式 # 版本21暂时还没法设置error的format access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s' # 重定向标准错误或者标准输出 capture_output = True # 设置日志级别 loglevel = 'info'
3. 启动gunicorn
gunicorn -c gunicorn.conf.py main:app
其中,gunicorn.conf.py是gunicorn的配置文件,main是运行的主文件main.py,app是main.py文件中的应用名称。

浙公网安备 33010602011771号