UWSGI 杀死工人的速度太快了

UWSGI killing workers too fast

我在我的 webapp 中遇到了一个错误,该错误已经运行了一年多,当我在新实例上切换到 UWSGI 以加快速度时,我遇到了这个错误。

我的应用程序有 "quick add" 模式 window 允许用户将新客户添加到数据库,并立即转到该用户的购物车。因此,模块向 /customers/quick_create/ 发出 POST 请求,该请求会重定向到 /cart/10000,其中 10000 是客户的 ID。然后乐趣开始。

由于检查 /cart 以查看是否有具有该 ID 的客户,我注意到检查被激活,并且在发出该请求时,用户被重定向到后备link,到实际的购物车。这是执行检查的代码:

q = Customer.query.filter_by(id=cust).first()
            if q is None:
                return redirect('/customers/')

支票在那里是因为有人可能不仅通过那个模态到达那个阶段。而且,有时用户会进入回退 url,有时会进入 /cart。在所有情况下,客户实际上是创建的,我可以稍后去它并在数据库中看到它,所以由于某种原因,这个SQL查询没有找到具有该 ID 的客户。

我检查了 USGI 日志,这是一个简短的摘录:

/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/default.py:324: Warning: Data truncated for column 'refill_date' at row 1
  cursor.execute(statement, parameters)
[pid: 5197|app: 0|req: 1/1] 123.123.123.123 () {54 vars in 1285 bytes} [Tue Feb  3 14:34:59 2015] POST /customers/quick_create/ => generated 237 bytes in 43 msecs (HTTP/1.1 302) 4 headers in 421 bytes (2 switches on core 0)
Tue Feb  3 14:34:59 2015 - ...The work of process 5197 is done. Seeya!
[pid: 5200|app: 0|req: 1/2] 123.123.123.123 () {48 vars in 1118 bytes} [Tue Feb  3 14:35:00 2015] GET /cart/16198/ => generated 229 bytes in 42 msecs (HTTP/1.1 302) 4 headers in 417 bytes (1 switches on core 0)
Tue Feb  3 14:35:00 2015 - ...The work of process 5200 is done. Seeya!
Tue Feb  3 14:35:00 2015 - worker 1 killed successfully (pid: 5197)
Tue Feb  3 14:35:00 2015 - Respawned uWSGI worker 1 (new pid: 5218)
Tue Feb  3 14:35:00 2015 - worker 4 killed successfully (pid: 5200)
Tue Feb  3 14:35:00 2015 - Respawned uWSGI worker 4 (new pid: 5219)
Tue Feb  3 14:35:00 2015 - mapping worker 4 to CPUs: 0
Tue Feb  3 14:35:00 2015 - mapping worker 1 to CPUs: 0
Tue Feb  3 14:35:03 2015 - WSGI app 0 (mountpoint='') ready in 3 seconds on interpreter 0x1dd1630 pid: 5219 (default app)
Tue Feb  3 14:35:03 2015 - mounting uwsgi on /
Tue Feb  3 14:35:03 2015 - WSGI app 0 (mountpoint='') ready in 3 seconds on interpreter 0x1dd1630 pid: 5218 (default app)
Tue Feb  3 14:35:03 2015 - mounting uwsgi on /
[pid: 5199|app: 0|req: 1/3] 123.123.123.123 () {48 vars in 1110 bytes} [Tue Feb  3 14:35:00 2015] GET /customers/ => generated 3704543 bytes in 3402 msecs (HTTP/1.1 200) 3 headers in 370 bytes (18 switches on core 0)
Tue Feb  3 14:35:03 2015 - ...The work of process 5199 is done. Seeya!
Tue Feb  3 14:35:04 2015 - worker 3 killed successfully (pid: 5199)
Tue Feb  3 14:35:04 2015 - Respawned uWSGI worker 3 (new pid: 5226)
Tue Feb  3 14:35:04 2015 - mapping worker 3 to CPUs: 0
Tue Feb  3 14:35:05 2015 - WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x1dd1630 pid: 5226 (default app)
Tue Feb  3 14:35:05 2015 - mounting uwsgi on /

这是我的 UWSGI 配置:

 <uwsgi>
     <plugin>python</plugin>
     <socket>/run/uwsgi/app/example.com/example.com.socket</socket>
     <pythonpath>/srv/www/example.com/application/</pythonpath>
     <app mountpoint="/">
         <script>uwsgi</script>
     </app>

     <master/>
     <callable>app</callable>
     <module>app</module>
     <processes>4</processes>
     <harakiri>60</harakiri>
     <reload-mercy>8</reload-mercy>
     <cpu-affinity>1</cpu-affinity>
     <stats>/tmp/stats.socket</stats>
     <max-requests>2000</max-requests>
     <limit-as>512</limit-as>
     <reload-on-as>256</reload-on-as>
     <reload-on-rss>192</reload-on-rss>
     <no-orphans/>
     <vacuum/>
     <lazy-apps/>
 </uwsgi>

我真的很奇怪,UWSGI 在请求后立即杀死了那个工人。当有一些静态文件的请求时,每个请求都有新的PID。

mod_wsgi Apache 实例不会发生这种情况。 CPU 有时会达到 100%,但平均负载还可以,0.25, 0.22, 0.15 在撰写本文时,RAM 使用率约为 900 MB 中的 300。

谁能给我指出正确的方向?谢谢

运行进入这个问题:uwsgi cheaper killing workers processing requests

错误修复并于 2016 年 8 月合并。

https://github.com/unbit/uwsgi/issues/1288

https://github.com/unbit/uwsgi/commit/f5dd0b855d0d21be62534dc98375fae2cd7c13f0