I am building a server on sockjs-tornado, and wonder how could one take advantage of tornado's asynchronous HTTP client -- or other asynchronous facilities for tornado such as asyncmongo, tornado-redis, etc. Apparently it is not possible to use the tornado.web.asynchronous
& tornado.gen.engine
decorators on random methods. So if I need to do asynchronous Mongo/HTTP/Redis calls from within SockJSConnection's on_message()
, how would I do that?
All you have to do is to create a method (or a function) which is decorated by tornado.gen decorator
Created small gist to illustrate how you can do it: https://gist.github.com/3708549
If you will run sample and check server console, you'll see following output:
1 - Making request
2 - Returned from on_message
... slight delay ...
3 - Sent data to client
So, it is not blocking ioloop and makes HTTP call in background.
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