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

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

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


当前回答

会是这样吗? http://docs.gunicorn.org/en/latest/settings.html#timeout

其他的可能是你的回复时间太长或者被困在等待中。

其他回答

对我来说,这是因为我忘记在数据库服务器上为我的Django设置防火墙规则。

关于在Azure应用服务(Linux应用)上运行Flask Apps的Microsoft Azure官方文档声明超时时间为600

gunicorn --bind=0.0.0.0 --timeout 600 application:app

https://learn.microsoft.com/en-us/azure/app-service/configure-language-python#flask-app

除了已经建议的gunicorn超时设置,因为你在前面使用nginx,你可以检查这两个参数是否有效,proxy_connect_timeout和proxy_read_timeout默认为60秒。可以在nginx配置文件中这样设置它们,

proxy_connect_timeout 120s;
proxy_read_timeout 120s;

你需要使用另一个worker类型类,比如gevent或tornado。 第一个解释:

如果您预计应用程序代码在请求处理期间可能需要暂停较长时间,您可能还需要安装Eventlet或Gevent

第二点:

默认的同步工作线程假定您的应用程序在CPU和网络带宽方面受到资源限制。通常这意味着您的应用程序不应该执行任何花费未定义时间的操作。例如,对互联网的请求就符合这个标准。在某些时候,外部网络会出现故障,客户端会堆积在您的服务器上。

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

所以我建议你先检查一下是什么减慢了你的应用程序