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?
Aside from data storage, Redis can be used as a Publisher/Subscriber platform. In this pattern, publishers can issue messages to any number of subscribers on a channel.
No - Redis' Pub/Sub has no persistence, and once a message has been published, it is sent only to the connected subscribed clients.
Redis Pub/Sub is designed for speed (low latency), but only with low numbers of subscribers —subscribers don't poll and while subscribed/connected are able to receive push notifications very quickly from the Redis broker—in the low ms, even < 1ms as confirmed by this benchmark.
Redis Pub/Sub implements the messaging system where the senders (in redis terminology called publishers) sends the messages while the receivers (subscribers) receive them. The link by which the messages are transferred is called channel. In Redis, a client can subscribe any number of channels.
If you're thinking of non-blocking, asynchronous processing, you're probably using (or should use) asynchronous framework/server.
if you're using Tornado, there is Tornado-Redis. It's using native Tornado generator calls. Its Websocket demo provides example on how to use it in combination with pub/sub.
if you're using Twisted, there is txRedis. There you also have pub/sub example.
it also seems that you can use Redis-py combined with Gevent with no problems using Gevent's monkey patching (gevent.monkey.patch_all()
).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With