我已经设置了gunicorn与3个工人,30个工人连接和使用eventlet工人类。它被设置在Nginx后面。每请求几次,我就会在日志里看到这个。

[ERROR] gunicorn.error: WORKER TIMEOUT (pid:23475)
None
[INFO] gunicorn.error: Booting worker with pid: 23514

为什么会这样?我怎样才能知道哪里出了问题呢?


当前回答

我在Docker中也遇到了同样的问题。

在Docker中,我保持训练过的LightGBM模型+ Flask服务请求。作为HTTP服务器,我使用gunicorn 19.9.0。当我在我的Mac笔记本电脑上本地运行我的代码时,一切都很完美,但当我在Docker中运行应用程序时,我的POST JSON请求冻结了一段时间,然后gunicorn工人已经失败了[CRITICAL]工人超时异常。

我尝试了大量不同的方法,但唯一解决我的问题的是添加worker_class=gthread。

以下是我的完整配置:

import multiprocessing

workers = multiprocessing.cpu_count() * 2 + 1
accesslog = "-" # STDOUT
access_log_format = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(q)s" "%(D)s"'
bind = "0.0.0.0:5000"
keepalive = 120
timeout = 120
worker_class = "gthread"
threads = 3

其他回答

使用——log-level debug运行Gunicorn。

它应该会给你一个应用程序堆栈跟踪。

对我来说,最简单的方法是在你的app.py存在的文件夹中创建一个新的config.py文件,并在其中放入超时和所有你想要的特殊配置:

timeout = 999

然后在指向这个配置文件的同时运行服务器

gunicorn -c config.py --bind 0.0.0.0:5000 wsgi:app

注意,要使这条语句工作,还需要将wsgi.py放在具有以下内容的同一目录中

from myproject import app

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

干杯!

以我为例,我在向服务器发送较大(10MB)文件时遇到了这个问题。我的开发服务器(app.run())收到他们没有问题,但gunicorn无法处理他们。

和我遇到同样问题的人。我的解决方案是像这样把它分成块发送: 参考/ HTML的例子,单独的大文件参考

def upload_to_server():
    upload_file_path = location

    def read_in_chunks(file_object, chunk_size=524288):
        """Lazy function (generator) to read a file piece by piece.
        Default chunk size: 1k."""
        while True:
            data = file_object.read(chunk_size)
            if not data:
                break
            yield data

    with open(upload_file_path, 'rb') as f:
        for piece in read_in_chunks(f):
            r = requests.post(
                url + '/api/set-doc/stream' + '/' + server_file_name,
                files={name: piece},
                headers={'key': key, 'allow_all': 'true'})

我的烧瓶服务器:

@app.route('/api/set-doc/stream/<name>', methods=['GET', 'POST'])
def api_set_file_streamed(name):
    folder = escape(name)  # secure_filename(escape(name))
    if 'key' in request.headers:
        if request.headers['key'] != key:                
            return 404
    else:
        return 404
    for fn in request.files:
        file = request.files[fn]
        if fn == '':
            print('no file name')
            flash('No selected file')
            return 'fail'
        if file and allowed_file(file.filename):
            file_dir_path = os.path.join(app.config['UPLOAD_FOLDER'], folder)
            if not os.path.exists(file_dir_path):
                os.makedirs(file_dir_path)
            file_path = os.path.join(file_dir_path, secure_filename(file.filename)) 
            with open(file_path, 'ab') as f:
                f.write(file.read())
            return 'sucess'
    return 404

我有非常相似的问题,我也尝试使用“运行服务器”,看看我是否能找到任何东西,但我所拥有的只是一个消息杀死

所以我认为这可能是资源问题,我继续给实例更多的RAM,它工作了。

超时是这个问题的一个关键参数。

然而,它不适合我。

当我设置workers=1时,我发现没有gunicorn超时错误。

当我看我的代码,我发现一些套接字连接(套接字。在服务器init中发送& socket.recv)。

套接字。Recv将阻塞我的代码,这就是为什么它总是超时时,工人>1

希望能给那些对我有意见的人一些建议