Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous calls from within sockjs-tornado methods

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?

like image 675
Costas Kotsokalis Avatar asked Feb 19 '23 04:02

Costas Kotsokalis


1 Answers

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.

like image 160
Joes Avatar answered Mar 05 '23 02:03

Joes