Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is non-blocking Redis pubsub possible?

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?

like image 686
limboy Avatar asked Oct 24 '11 05:10

limboy


People also ask

Is Redis good for Pubsub?

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.

Is Redis pub/sub persistent?

No - Redis' Pub/Sub has no persistence, and once a message has been published, it is sent only to the connected subscribed clients.

Is Redis pub/sub fast?

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.

How do I use Redis as a pub sub?

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.


1 Answers

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.

like image 91
vartec Avatar answered Sep 22 '22 11:09

vartec