UWSGI杀死工人太快了

时间:2015-02-03 19:44:19

标签: python sql flask uwsgi worker

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

我的应用程序有“快速添加”模式窗口,允许用户将新客户添加到数据库,并立即转到该用户的购物车。因此,模块向POST发出/customers/quick_create/请求,该请求会重定向到/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。

有人能指出我正确的方向吗?感谢