Gunicorn真折磨人
今天帮FursuitGuide写了网站后端,用的是Python的FastAPI
到部署的时候,Supervisor,Gunicorn和Uvicorn给了我邦邦三拳
部署Gunicorn+Uvicorn
首先呢,假设我们程序目录为: /www/Project/Program/main.py
程序内为:
1 2 3 4 5 6 7
| from fastapi import FastAPI
app = FastAPI()
@app.get("/") def read_root(): return {"Hello": "World"}
|
Venv路径为: /www/Project/Program/venv
安装了Gunicorn,Uvicorn和FastAPI
现在,在 /www/Project/Program/gconfig.py 中写入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| import multiprocessing
bind = "0.0.0.0:8000"
workers = multiprocessing.cpu_count() * 2 + 1
backlog = 2048
worker_class = "uvicorn.workers.UvicornWorker"
daemon = 'false'
worker_connections = 2000
loglevel = 'info'
errorlog = '-' accesslog = '-'
logconfig_dict = { 'formatters': { "generic": { "format": "%(process)d %(asctime)s %(levelname)s %(message)s", "datefmt": "[%Y-%m-%d %H:%M:%S %z]", "class": "logging.Formatter" } } }
|
现在呢,你就可以运行以下代码来测试配置了(?):
1
| /www/Project/Program/venv/bin/gunicorn -c=gconfig.py main:app
|
部署Supervisor
在你的Supervisor子配置中输入:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| [program:Program] command=/www/Project/Program/venv/bin/gunicorn -c=gconfig.py main:app directory=/www/Project/Program autorestart=true startsecs=3 startretries=3 stdout_logfile=/supervisor/log/Program.out.log stderr_logfile=/supervisor/log/Program.err.log stdout_logfile_maxbytes=2MB stderr_logfile_maxbytes=2MB user=www priority=999 numprocs=1 process_name=%(program_name)s_%(process_num)02d
|
然后运行Supervisor就可以了(?)
总结
一个字: 菜