我正在使用gevent来构建一个服务器,它做一些redis的工作,并将结果返回给客户端。但是性能很差。经过一些研究,我发现和redis只有一种联系。看起来只有一个greenlet生成了。下面是我的程序:
#!/usr/bin/env python
from gevent import monkey
monkey.patch_all()
import gevent
from gevent.wsgi import WSGIServer
from gevent.pool import Pool
import gevent.socket
from cgi import parse_qs, escape
import json
import redis
p = redis.ConnectionPool()
def app(environ, start_response):
status = '200 OK'
body = ''
path = environ.get('PATH_INFO', '').lstrip('/')
parameters = parse_qs(environ.get('QUERY_STRING', ''))
r = redis.Redis(connection_pool=p)
if path == 'top':
l = r.zrevrange('online', 0, 50, True)
body = json.dumps({'onlinetime':map(lambda (pid, online): {'pid':pid, 'onlinetime':online}, l)}) + '\n'
headers = [
('Content-Type', 'text/html'),
('Content-Length', str(len(body))),
]
print 'in_use_conn:', len(p._in_use_connections), 'created_connections:', p._created_connections
start_response(status, headers)
yield body
def main():
pool = Pool(1000)
WSGIServer(('', 7332), app, spawn=pool, log=None).serve_forever()
if __name__ == '__main__':
main()我的程序有什么问题吗?为什么只有一个连接到redis?
发布于 2013-10-01 21:18:51
看一看http://gehrcke.de/2013/01/highly-concurrent-connections-to-redis-with-gevent-and-redis-py/
我不是100%是你的猴子补丁在起作用,但我会把它替换成:
import gevent
import redis.connection
redis.connection.socket = gevent.socket你也可以创建你自己的池,使用gevent支持的连接到redis……
发布于 2013-10-02 00:04:54
是什么让你觉得你和redis只有一种联系?实际上,我的小测试显示您的服务器确实打开了很多到redis的连接。
为了使测试更清楚,我对print语句进行了一些修改:
print '%s' % parameters['index'], 'in_use_conn:', len(p._in_use_connections), 'created_connections:', p._created_connections, 'available_conn:', len(p._available_connections)然后运行此脚本发出一些请求:
for i in {1..20}
do
wget http://127.0.0.1:7332/top?index=$i > /dev/null 2>&1 &
done下面是我得到的信息:
['1'] in_use_conn: 1 created_connections: 2 available_conn: 1
['2'] in_use_conn: 4 created_connections: 5 available_conn: 1
['3'] in_use_conn: 3 created_connections: 5 available_conn: 2
['4'] in_use_conn: 5 created_connections: 6 available_conn: 1
['6'] in_use_conn: 4 created_connections: 6 available_conn: 2
['5'] in_use_conn: 3 created_connections: 6 available_conn: 3
['7'] in_use_conn: 2 created_connections: 6 available_conn: 4
['10'] in_use_conn: 1 created_connections: 6 available_conn: 5
['8'] in_use_conn: 0 created_connections: 6 available_conn: 6
['14'] in_use_conn: 10 created_connections: 11 available_conn: 1
['11'] in_use_conn: 9 created_connections: 11 available_conn: 2
['12'] in_use_conn: 8 created_connections: 11 available_conn: 3
['16'] in_use_conn: 7 created_connections: 11 available_conn: 4
['15'] in_use_conn: 6 created_connections: 11 available_conn: 5
['13'] in_use_conn: 5 created_connections: 11 available_conn: 6
['20'] in_use_conn: 4 created_connections: 11 available_conn: 7
['19'] in_use_conn: 3 created_connections: 11 available_conn: 8
['9'] in_use_conn: 2 created_connections: 11 available_conn: 9
['17'] in_use_conn: 1 created_connections: 11 available_conn: 10
['18'] in_use_conn: 0 created_connections: 11 available_conn: 11可以看到,在peek时间,您有10个greenlet同时运行,等待套接字。在我看来,您的代码看起来非常好。为什么“表现不佳”是另一回事。这可能是你的“在线”排序集太大了。或者更有可能的是,您正在使用阻塞客户端来测试服务器,在这种情况下,您将只看到一个到redis的连接。
https://stackoverflow.com/questions/15699666
复制相似问题