gunicorn 使用
方式一:最简单的使用 pip 安装或者更新
pip install gunicorn 有些离线安装的场景也可心到 https://pypi.org/project/gunicorn/ 下载whl包,然后在同一目录下运行 pip install gunicorn-XXX.whl
方式二:下载源码安装
git clone git://github.com/benoitc/gunicorn.git cd gunicorn sudo python setup.py install
配置
py配置文件
# gunicorn_config.py
#!/usr/bin/env python
# -*- coding: utf-8 -*
# Author: renoyuan
# e_mail: renoyuan@foxmail.com
import logging
import logging.handlers
from logging.handlers import WatchedFileHandler
import os
import multiprocessing
bind = '0.0.0.0:7088' # 绑定ip和端口号
backlog = 512 # 监听队列
chdir = '/root/zj/HTTP_SET' # gunicorn要切换到的目的工作目录
timeout = 30 # 超时 s
worker_class = 'gevent' # 使用gevent模式,还可以使用 sync 模式,默认的是sync模式
daemon = True # 后台运行 默认False
# worker_class = 'sync'
pidfile = f"{chdir}/gunicorn.pid"
workers = 3 # 进程数
# workers = multiprocessing.cpu_count() * 2 + 1 #进程数
threads = 1 # 指定每个进程开启的线程数
loglevel = 'info' # 日志级别,这个日志级别指的是错误日志的级别,而访问日志的级别无法设置
access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"' # 设置gunicorn访问日志格式,错误日志无法设置
"""
其每个选项的含义如下:
h 地址
l '-'
u currently '-', may be user name in future releases
t 日期时间
r 接口信息
s http status
b 响应长度 or '-'
f referer
a 用户代理
T 请求耗时/s
D 请求耗时/毫秒
L 请求耗时精确地/s
p 进程 ID
"""
accesslog = f"{chdir}/gunicorn_access.log" # 访问日志文件
errorlog = f"{chdir}/gunicorn_error.log" # 错误日志文件
ini 配置文件
[server:main]
use = egg:gunicorn#main
host = 192.168.0.1
port = 80
workers = 2
proc_name = brim
运行
方式一
nohup gunicorn -c gunicorn_config.py http_test_main:app &
方式二
gunicorn -c gunicorn_config.py -D http_test_main:app
参数含义
// --version 查看版本
// -h 查看帮助文档
// -c or --config 加载配置文件
// -D or daemon 后台运行
// -p or --pid FILE 指定pid 文件
// 加载wsgi 对象 flask 框架为app 对象DJANGO 为 appName.wsgi:application
http_test_main:app
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)
· AI 智能体引爆开源社区「GitHub 热点速览」