Skip to content Skip to sidebar Skip to footer

Why Only One Connection To Redis Was Made In This Gevent Program?

I'm using gevent to build a server which do some redis stuff and return the result to client. But the performance is bad. After some research I found that there is only one connect

Solution 1:

Have a look at http://gehrcke.de/2013/01/highly-concurrent-connections-to-redis-with-gevent-and-redis-py/

I'm not 100% is your monkey-patching is doing the trick but I'd replace it with:

import gevent
import redis.connection
redis.connection.socket = gevent.socket

You could also go and create your own pool with gevent supported connection to redis...

Solution 2:

What makes you think your only have one connection to redis? Actually my little test shows that your server is indeed opening lots of connections to redis.

To make the test more clear, I modified your print statement a bit:

print'%s' % parameters['index'], 'in_use_conn:', len(p._in_use_connections), 'created_connections:', p._created_connections, 'available_conn:', len(p._available_connections)

Then run this script to make some requests:

for i in {1..20}
do
    wget http://127.0.0.1:7332/top?index=$i > /dev/null 2>&1 &
done

And here's what I got:

['1']in_use_conn: 1created_connections: 2available_conn: 1['2']in_use_conn: 4created_connections: 5available_conn: 1['3']in_use_conn: 3created_connections: 5available_conn: 2['4']in_use_conn: 5created_connections: 6available_conn: 1['6']in_use_conn: 4created_connections: 6available_conn: 2['5']in_use_conn: 3created_connections: 6available_conn: 3['7']in_use_conn: 2created_connections: 6available_conn: 4['10']in_use_conn: 1created_connections: 6available_conn: 5['8']in_use_conn: 0created_connections: 6available_conn: 6['14']in_use_conn: 10created_connections: 11available_conn: 1['11']in_use_conn: 9created_connections: 11available_conn: 2['12']in_use_conn: 8created_connections: 11available_conn: 3['16']in_use_conn: 7created_connections: 11available_conn: 4['15']in_use_conn: 6created_connections: 11available_conn: 5['13']in_use_conn: 5created_connections: 11available_conn: 6['20']in_use_conn: 4created_connections: 11available_conn: 7['19']in_use_conn: 3created_connections: 11available_conn: 8['9']in_use_conn: 2created_connections: 11available_conn: 9['17']in_use_conn: 1created_connections: 11available_conn: 10['18']in_use_conn: 0created_connections: 11available_conn: 11

It can be seen that at peek time you have 10 greenlets running simultaneously, waiting for sockets. Your code looks perfectly fine to me. Why 'the performance is bad' is another story. It could be your sorted set of 'online' is tooo large. Or more likely you are using a blocking client to test the server, in which case you'll see only one connection to redis.

Post a Comment for "Why Only One Connection To Redis Was Made In This Gevent Program?"