【问题标题】:UWSGI killing workers too fastUWSGI 杀死工人太快了
【发布时间】:2015-04-03 03:35:53
【问题描述】:

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

我的应用程序有“快速添加”模式窗口,允许用户将新客户添加到数据库,并立即进入该用户的购物车。因此,模块向/customers/quick_create/ 发出POST 请求,该请求重定向到/cart/10000,其中10000 是客户的ID。然后乐趣就开始了。

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

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

检查存在是因为有人可能不仅通过该模式到达该阶段。而且,有时用户会访问备用网址,有时会访问/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。

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

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

【问题讨论】:

  • 在确切知道所需大小之前不要限制内存。并避免在 64 位机器上限制地址空间(我不知道是不是你的情况)。最后(不相关) 选项自 2012 年以来已被弃用,此后不再使用。

标签: python sql flask uwsgi worker


【解决方案1】:

【讨论】:

  • 这是不久前的事了,但我很高兴看到它解决了!
猜你喜欢
  • 2014-10-14
  • 1970-01-01
  • 2010-12-31
  • 1970-01-01
  • 2019-12-15
  • 1970-01-01
  • 1970-01-01
  • 2016-06-30
  • 2020-03-19
相关资源
最近更新 更多