Skip to content Skip to sidebar Skip to footer

Tornado One Handler Blocks For Another

Using python/tornado I wanted to set up a little 'trampoline' server that allows two devices to communicate with each other in a RESTish manner. There's probably vastly superior/si

Solution 1:

1) How can one HTTP verb invocation (e.g. get, put, post,u ne etc) block and then be signaled by another HTTP verb invocation.

2) How can I share data from one invocation to another?

The new RequestHandler object is created for every request. So you need some coordinator e.g. queues or locks with state object (in your case it would be re-implementing queue).

tornado.queues are queues for coroutines. Queue.get, Queue.put, Queue.join return Future objects, that need to be "resolved" - scheduled task done either with success or exception. To wait until future is resolved you should yielded it (just like in the doc examples of tornado.queues). The verbs method also need to be decorated with tornado.gen.coroutine.

import tornado.gen

classQuery(tornado.web.RequestHandler):

    @tornado.gen.coroutinedefget(self):
        toServer = yield ToServerQueue.get()
        self.write(toServer)

    @tornado.gen.coroutinedefpost(self):
        toServer = self.request.body
        yield ToServerQueue.put(toServer)
        toClient = yield ToClientQueue.get()
        self.write(toClient)

    @tornado.gen.coroutinedefput(self):
        yield ToClientQueue.put(self.request.body)
        self.write(bytes())

The GET request will last (wait in non-blocking manner) until something will be available on the queue (or timeout that can be defined as Queue.get arg).

tornado.queues.Queue provides also get_nowait (there is put_nowait as well) that don't have to be yielded - returns immediately item from queue or throws exception.

Post a Comment for "Tornado One Handler Blocks For Another"