gunicorn 进程不断重启,CRITICAL WORKER TIMEOUT

记录一个奇怪的问题,暂时解决,但是没有找到根本原因,有空再回来解决



# gunicornconf.py
import os

home = '/home/ops'
print('home:'+home)
bind = '0.0.0.0:8000'      #绑定ip和端口号
backlog = 512                #监听队列
chdir = home+'/server/bin'  #gunicorn要切换到的目的工作目录
timeout = 30      #超时
worker_class = 'sync' #使用gevent模式,还可以使用sync 模式,默认的是sync模式

workers =  3 #+ multiprocessing.cpu_count() * 2   #进程数
threads = 2 #指定每个进程开启的线程数
loglevel = 'debug' #日志级别,这个日志级别指的是错误日志的级别,而访问日志的级别无法设置
access_log_format = '%(t)s %(p)s %(h)s "%(r)s" %(s)s %(L)s %(b)s %(f)s" "%(a)s"'    #设置gunicorn访问日志格式,错误日志无法设置

"""
其每个选项的含义如下:
h          remote address
l          '-'
u          currently '-', may be user name in future releases
t          date of the request
r          status line (e.g. ``GET / HTTP/1.1``)
s          status
b          response length or '-'
f          referer
a          user agent
T          request time in seconds
D          request time in microseconds
L          request time in decimal seconds
p          process ID
"""
accesslog = home+"/server/log/gunicorn_access.log"      #访问日志文件
errorlog = home+"/server/log/gunicorn_error.log"        #
#flaskapi.py
import sys
import os
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
sys.path.append(rootPath)
print('rootPath:'+rootPath)
print('syspath:' +str(sys.path))
from flask import Flask
import logging
from chatbox.chatmodel import retrieval_milvus
logging.basicConfig(format='%(levelname)s:%(funcName)s:%(message)s', level = logging.DEBUG)


def create_app():
    app = Flask(__name__)
    return app

app = create_app()

@app.route('/chatbots/<words>')
def get_answer(words):
    try:
        print(words)
        try:
            answer_id= retrieval_milvus(words)
        except Exception as E:
            logging.error('exception:' + str(E))
            return '留言转接中,请稍等。。。'
        return answer_id
    except Exception as E:
        logging.error('exception:' + str(E))
    return 'Error'

if __name__ == '__main__':
    app.run()

 现象: gunicorn 进程不断重启,CRITICAL WORKER TIMEOUT 

查看下面issue后尝试也没有找到办法,但是修改worker_class后暂时能跑了,本来是gevent模式,改成worker_class = 'sync'后能跑了,原因未知,有空回来看

worker_class = 'sync' #使用gevent模式,还可以使用sync 模式,默认的是sync模式


CRITICAL WORKER TIMEOUT when running Flask app · Issue #1801 · benoitc/gunicorn · GitHubicon-default.png?t=L9C2https://hub.fastgit.org/benoitc/gunicorn/issues/1801

### Gunicorn 命令行参数用法 Gunicorn 是一个 Python WSGI HTTP Server,用于运行基于 WSGI 的 Web 应用程序。以下是关于 Gunicorn 命令行参数的一些重要知识点: #### 1. 启动 Gunicorn 要启动 Gunicorn 并使其运行一个 WSGI 应用程序,可以使用以下基本命令结构: ```bash gunicorn [OPTIONS] APP_MODULE ``` 其中 `APP_MODULE` 表示应用程序模块的位置,通常是一个 Python 文件中的函数名,例如 `myapp:app`。 --- #### 2. 常见命令行参数 - **绑定地址和端口** 使用 `-b` 或者 `--bind` 参数指定监听的 IP 地址和端口号,默认为 `127.0.0.1:8000`。 ```bash gunicorn --bind 0.0.0.0:8000 myapp:app ``` - **工作线程数** 使用 `-w` 或者 `--workers` 设置并发的工作进程数量。推荐设置为 `(CPU 核心数 * 2) + 1`。 ```bash gunicorn --workers=4 myapp:app ``` - **工作模式** 使用 `-k` 或者 `--worker-class` 指定工作类(同步、异步等)。常见的选项包括 `sync`, `gevent`, 和 `eventlet`。 ```bash gunicorn --worker-class=gunicorn.workers.gthread.ThreadingWorker myapp:app ``` - **日志级别** 使用 `--log-level` 调整日志记录的详细程度,支持 `debug`, `info`, `warning`, `error`, 和 `critical`。 ```bash gunicorn --log-level debug myapp:app ``` - **超时时间** 使用 `--timeout` 配置每个请求的最大处理时间(秒),超过该时间会终止连接。 ```bash gunicorn --timeout 30 myapp:app ``` - **守护进程化** 使用 `-D` 或者 `--daemon` 让 Gunicorn 在后台运行。 ```bash gunicorn --daemon myapp:app ``` - **自定义配置文件** 使用 `-c` 或者 `--config` 加载外部配置文件。 ```bash gunicorn --config=config.py myapp:app ``` --- #### 3. 错误排查与调试 如果遇到 `"No module named <module_name>"` 类型的错误[^2],可能是因为当前环境中未正确安装依赖项或 Gunicorn 版本不匹配。可以通过以下方式解决: - 确认虚拟环境已激活并包含所需包; - 明确指定 Gunicorn 所属的虚拟环境路径; - 编辑 `bin/gunicorn` 中的第一行为正确的 Python 解释器路径。 对于 `bad interpreter` 错误,需手动调整脚本头部解释器指向实际可用版本。 --- #### 4. 实际案例参考 假设有一个 Flask 应用保存在名为 `hello.py` 的文件中,入口点为 `app` 函数,则可通过如下命令启动服务: ```bash gunicorn -w 4 -b 0.0.0.0:5000 hello:app ``` 更多复杂场景部署指南可查阅官方文档或其他社区资源[^3]。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值