在线上发现python-memcached客户端很有问题,我在本地通过pthon manager runserver测试,刷新用户首页就有大量的TIME_WAIT:
yunpeng@yunpeng-duitang:/duitang/dist/conf/redis$ netstat -an | grep 11211
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:53169 127.0.0.1:11211 TIME_WAIT
tcp 0 0 127.0.0.1:53315 127.0.0.1:11211 TIME_WAIT
tcp 0 0 127.0.0.1:53293 127.0.0.1:11211 TIME_WAIT
tcp 0 0 127.0.0.1:53161 127.0.0.1:11211 TIME_WAIT
tcp 0 0 127.0.0.1:53306 127.0.0.1:11211 TIME_WAIT
tcp 0 0 127.0.0.1:53308 127.0.0.1:11211 TIME_WAIT
tcp 0 0 127.0.0.1:53283 127.0.0.1:11211 TIME_WAIT
tcp 0 0 127.0.0.1:53262 127.0.0.1:11211 TIME_WAIT
tcp 0 0 127.0.0.1:53163 127.0.0.1:11211 TIME_WAIT
tcp 0 0 127.0.0.1:53284 127.0.0.1:11211 TIME_WAIT
tcp 0 0 127.0.0.1:53139 127.0.0.1:11211 TIME_WAIT
tcp 0 0 127.0.0.1:53140 127.0.0.1:11211 TIME_WAIT
tcp 0 0 127.0.0.1:53264 127.0.0.1:11211 TIME_WAIT
tcp 0 0 127.0.0.1:53157 127.0.0.1:11211 TIME_WAIT
tcp 0 0 127.0.0.1:53167 127.0.0.1:11211 TIME_WAIT
tcp 0 0 127.0.0.1:53173 127.0.0.1:11211 TIME_WAIT
tcp 0 0 127.0.0.1:53177 127.0.0.1:11211 TIME_WAIT
tcp 0 0 127.0.0.1:53209 127.0.0.1:11211 TIME_WAIT
tcp 0 0 127.0.0.1:53159 127.0.0.1:11211 TIME_WAIT
tcp 0 0 127.0.0.1:53203 127.0.0.1:11211 TIME_WAIT
经测试发现django在每次请求请求之后都会关闭socket,导致大量TIME_WAIT,试着把memcache.py的disconnect_all注释掉,大量TIME_WAIT消失。
yunpeng@yunpeng-duitang:/duitang/dist/conf/redis$ netstat -an | grep 11211
tcp 0 0 0.0.0.0:11211 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:11211 127.0.0.1:53352 ESTABLISHED
tcp 0 0 127.0.0.1:53352 127.0.0.1:11211 ESTABLISHED
但是我去线上机器测试发现又不太一样。
1. gunicorn开启一个进程
刷新个人主页,memcached连接数
[admin@server1 ~]$ netstat -an | grep "ESTABLISHED" |grep 11211 | wc -l
10
[admin@server1 ~]$ netstat -an | grep "TIME_WAIT" |grep 11211 | wc -l
236
注释掉disconnect_all:
[admin@server1 duitang]$ netstat -an | grep "TIME_WAIT" |grep 11211 | wc -l
322
[admin@server1 duitang]$ netstat -an | grep "ESTABLISHED" |grep 11211 | wc -l
11
2.使用uwsgi一个单个进程,2线程模式
[admin@server2 duitang]$ netstat -antp| grep 11211 | grep -v java | grep ESTABLISHED | wc -l
20
[admin@server2 duitang]$ netstat -antp| grep 11211 | grep -v java | grep TIME_WAIT | wc -l
0