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

 

 

 

 

原文地址:https://www.cnblogs.com/renoyuan/p/15667658.html