pip install uvicorn
pip install gunicorn
import os
# 设置守护进程
daemon=True
# 监听内网端口8000
bind='0.0.0.0:8000'
# 设置进程文件目录
pidfile='./gunicorn.pid'
chdir='./' # 工作目录
# 工作模式
worker_class='uvicorn.workers.UvicornWorker'
# 并行工作进程数 核心数*2+1个
workers=3 #multiprocessing.cpu_count()+1
# 指定每个工作者的线程数
threads=2
# 设置最大并发量
worker_connections = 2000
loglevel='debug' # 错误日志的日志级别
access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"'
# 设置访问日志和错误信息日志路径
log_dir = "./log"
if not os.path.exists(log_dir):
os.makedirs(log_dir)
accesslog = "./log/gunicorn_access.log"
errorlog = "./log/gunicorn_error.log"
gunicorn main:app -c gunicorn.py
#!/bin/bash
# 查找 gunicorn 主进程 PID
gunicorn_pid=$(ps aux | grep 'gunicorn' | grep -v 'grep' | awk '{print $2}')
# 如果找到了主进程 PID
if [ -n "$gunicorn_pid" ]; then
echo "Found gunicorn process: $gunicorn_pid"
# 给主进程发 SIGINT 信号,请求正常停止进程
kill -INT $gunicorn_pid
# 睡眠 5 秒等待主进程结束
sleep 5
# 查找所有 gunicorn 子进程 PID
gunicorn_child_pids=$(pstree -p $gunicorn_pid | grep -oP '([0-9]+)(?=\))')
# 如果找到了子进程 PID
if [ -n "$gunicorn_child_pids" ]; then
echo "Found gunicorn child processes: $gunicorn_child_pids"
# 杀死所有子进程
for pid in $gunicorn_child_pids; do
kill -9 $pid
done
fi
echo "Stopped gunicorn process and child processes"
else
echo "No running gunicorn process found"
fi
server {
listen 80;
# listen 443 ssl;
server_name api.hmily.vip;
access_log /var/log/nginx/access.log;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
}
}