skyork
skyork

Reputation: 7381

Use redis pub/sub to connect Tornado processes

I have two Tornado processes X and Y. A handler in X handles post requests, and when such a request arrives, in addition to making changes in X, I also want to alter some variables stored in Y.

I would like to use Redis's PUB/SUB to do this. It is obvious that, in the handler of X, I need to publish a message to the channel which is subscribed by 'Y'.

This subscription in Y should be long-running, so that whenever a message is published to the channel, something in Y should automatically update the variables in memory. However, it is not clear to me how to implement this subscription and the resulting updating in Y, as it can't be a normal handler. I plan to the asynchronous redis client tornado-redis for this, if that makes any difference.

Some example would be great help!

Upvotes: 1

Views: 782

Answers (1)

Eren Güven
Eren Güven

Reputation: 2374

This is not a Redis PUB/SUB solution but, alternatives. If you have Redis on your stack already, you might as well use that (or better yet a message queue).

  1. Easiest is to have a tornado.web.RequestHandler for this in Y. When X needs to make the change, have X send an HTTPRequest to Y. Payload can be JSON, form-encoded, binary.

  2. Create a socket within Y, bind it and add IOLoop.add_handler listening on that socket with a callback to run. When X needs to make the change, have it connect to that socket. (Payload can be JSON, binary). This would require a little socket accepting & reading if you need to send a message across.

Needless to say, both should be behind firewalls.

Upvotes: 1

Related Questions