Is non-blocking Redis pubsub possible?

limboy picture limboy · Oct 24, 2011 · Viewed 35.6k times · Source

I want to use redis' pubsub to transmit some messages, but don't want be blocked using listen, like the code below:

import redis
rc = redis.Redis()

ps = rc.pubsub()
ps.subscribe(['foo', 'bar'])

rc.publish('foo', 'hello world')

for item in ps.listen():
    if item['type'] == 'message':
        print item['channel']
        print item['data']

The last for section will block. I just want to check if a given channel has data, how can I accomplish this? Is there a check like method?

Answer

vartec picture vartec · Jan 8, 2013

If you're thinking of non-blocking, asynchronous processing, you're probably using (or should use) asynchronous framework/server.

UPDATE: It's been 5 years since the original answer, in the mean time Python got native async IO support. There now is AIORedis, an async IO Redis client.