Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the proper way to handle Redis connection in Tornado ? (Async - Pub/Sub)
    primarykey
    data
    text
    <p>I am using Redis along with my Tornado application with asyc client Brukva, when I looked at the sample apps at Brukva site they are making new connection on "<strong>init</strong>" method in websocket </p> <pre><code>class MessagesCatcher(tornado.websocket.WebSocketHandler): def __init__(self, *args, **kwargs): super(MessagesCatcher, self).__init__(*args, **kwargs) self.client = brukva.Client() self.client.connect() self.client.subscribe('test_channel') def open(self): self.client.listen(self.on_message) def on_message(self, result): self.write_message(str(result.body)) def close(self): self.client.unsubscribe('test_channel') self.client.disconnect() </code></pre> <p>its fine in the case of websocket but how to handle it in the common Tornado RequestHandler post method say long polling operation <strong>(publish-subscribe model)</strong>. I am making new client connetion in every post method of update handler is this the right approach ?? When I checked at the redis console I see that clients increasing in every new post operation.</p> <p><img src="https://i.stack.imgur.com/4HBEC.png" alt="enter image description here"></p> <p>Here is a sample of my code.</p> <pre><code>c = brukva.Client(host = '127.0.0.1') c.connect() class MessageNewHandler(BaseHandler): @tornado.web.authenticated def post(self): self.listing_id = self.get_argument("listing_id") message = { "id": str(uuid.uuid4()), "from": str(self.get_secure_cookie("username")), "body": str(self.get_argument("body")), } message["html"] = self.render_string("message.html", message=message) if self.get_argument("next", None): self.redirect(self.get_argument("next")) else: c.publish(self.listing_id, message) logging.info("Writing message : " + json.dumps(message)) self.write(json.dumps(message)) class MessageUpdatesHandler(BaseHandler): @tornado.web.authenticated @tornado.web.asynchronous def post(self): self.listing_id = self.get_argument("listing_id", None) self.client = brukva.Client() self.client.connect() self.client.subscribe(self.listing_id) self.client.listen(self.on_new_messages) def on_new_messages(self, messages): # Closed client connection if self.request.connection.stream.closed(): return logging.info("Getting update : " + json.dumps(messages.body)) self.finish(json.dumps(messages.body)) self.client.unsubscribe(self.listing_id) def on_connection_close(self): # unsubscribe user from channel self.client.unsubscribe(self.listing_id) self.client.disconnect() </code></pre> <p><strong>I appreciate if you provide some sample code for similar case.</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload