我已经设置了gunicorn与3个工人,30个工人连接和使用eventlet工人类。它被设置在Nginx后面。每请求几次,我就会在日志里看到这个。
[ERROR] gunicorn.error: WORKER TIMEOUT (pid:23475)
None
[INFO] gunicorn.error: Booting worker with pid: 23514
为什么会这样?我怎样才能知道哪里出了问题呢?
我已经设置了gunicorn与3个工人,30个工人连接和使用eventlet工人类。它被设置在Nginx后面。每请求几次,我就会在日志里看到这个。
[ERROR] gunicorn.error: WORKER TIMEOUT (pid:23475)
None
[INFO] gunicorn.error: Booting worker with pid: 23514
为什么会这样?我怎样才能知道哪里出了问题呢?
当前回答
WORKER TIMEOUT表示应用程序不能在规定的时间内响应请求。你可以使用gunicorn超时设置来设置。一些应用程序需要比另一个应用程序更多的时间来响应。
另一个可能影响这一点的因素是员工类型的选择
The default synchronous workers assume that your application is resource-bound in terms of CPU and network bandwidth. Generally this means that your application shouldn’t do anything that takes an undefined amount of time. An example of something that takes an undefined amount of time is a request to the internet. At some point the external network will fail in such a way that clients will pile up on your servers. So, in this sense, any web application which makes outgoing requests to APIs will benefit from an asynchronous worker.
当我遇到与您相同的问题时(我试图使用Docker Swarm部署我的应用程序),我尝试增加超时并使用另一种类型的工人类。但都失败了。
然后我突然意识到我的资源限制太低在我的撰写文件中的服务。在我的例子中,这就是减慢应用程序的原因
deploy:
replicas: 5
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
所以我建议你先检查一下是什么减慢了你的应用程序
其他回答
对我来说,解决方案是在我的入口点上添加——timeout 90,但它不起作用,因为我定义了两个入口点,一个在app.yaml中,另一个在Dockerfile中。我删除了未使用的入口点,并在另一个入口点添加了——timeout 90。
我在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
WORKER TIMEOUT表示应用程序不能在规定的时间内响应请求。你可以使用gunicorn超时设置来设置。一些应用程序需要比另一个应用程序更多的时间来响应。
另一个可能影响这一点的因素是员工类型的选择
The default synchronous workers assume that your application is resource-bound in terms of CPU and network bandwidth. Generally this means that your application shouldn’t do anything that takes an undefined amount of time. An example of something that takes an undefined amount of time is a request to the internet. At some point the external network will fail in such a way that clients will pile up on your servers. So, in this sense, any web application which makes outgoing requests to APIs will benefit from an asynchronous worker.
当我遇到与您相同的问题时(我试图使用Docker Swarm部署我的应用程序),我尝试增加超时并使用另一种类型的工人类。但都失败了。
然后我突然意识到我的资源限制太低在我的撰写文件中的服务。在我的例子中,这就是减慢应用程序的原因
deploy:
replicas: 5
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
所以我建议你先检查一下是什么减慢了你的应用程序
除了已经建议的gunicorn超时设置,因为你在前面使用nginx,你可以检查这两个参数是否有效,proxy_connect_timeout和proxy_read_timeout默认为60秒。可以在nginx配置文件中这样设置它们,
proxy_connect_timeout 120s;
proxy_read_timeout 120s;
我们在使用Django+nginx+gunicorn时也遇到了同样的问题。从Gunicorn文档中,我们配置了优雅的超时,几乎没有什么不同。
经过一些测试,我们找到了解决方案,要配置的参数是:timeout(并且不是优雅超时)。它走得像时钟一样快。
所以,做:
1)打开gunicorn配置文件
2)将TIMEOUT设置为您需要的任何值-以秒为单位
NUM_WORKERS=3
TIMEOUT=120
exec gunicorn ${DJANGO_WSGI_MODULE}:application \
--name $NAME \
--workers $NUM_WORKERS \
--timeout $TIMEOUT \
--log-level=debug \
--bind=127.0.0.1:9000 \
--pid=$PIDFILE