How to make Multiple Asynchronous Requests with tornado.gen.Task

2,448 views
Skip to first unread message

carl Cheng

unread,
Sep 2, 2013, 11:41:52 PM9/2/13
to python-...@googlegroups.com
Hi all,

with tornado.gen.Task, we can easily write AsyncHandler like the following code, but can anyone tell me how to make Multiple Asynchronous Requests with tornado.gen.Task ?  What should I add to the following code?

Thanks!


import tornado.ioloop
import tornado.web
import tornado.gen
import tornado.httpclient
 
 
class GenAsyncHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.engine
def get(self):
http_client = tornado.httpclient.AsyncHTTPClient()
response = yield tornado.gen.Task(http_client.fetch, "http://example.com")
self.finish('Fin.')
 
application = tornado.web.Application([
(r"/", GenAsyncHandler),
])
 
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()

Felinx Lee

unread,
Sep 3, 2013, 1:59:54 AM9/3/13
to python-...@googlegroups.com
There are some examples in gen documentation.

http://www.tornadoweb.org/en/stable/gen.html


--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
What can change the nature of a man?(Planescape Torment)
----------------------------------------------------------------------------------------
http://feilong.me            Felinx Lee's Blog (Chinese Only)

A. Jesse Jiryu Davis

unread,
Sep 3, 2013, 9:13:59 AM9/3/13
to python-...@googlegroups.com
In the modern style, yield a list of futures:

class GenAsyncHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    @tornado.gen.engine
    def get(self):
        http_client = tornado.httpclient.AsyncHTTPClient()
        future0 = http_client.fetch("http://example0.com")
        future1 = http_client.fetch("http://example1.com")
        result0, result1 = yield [future0, future1]
        self.finish('Fin.')

Waldecir Santos

unread,
Sep 3, 2013, 10:00:44 AM9/3/13
to python-...@googlegroups.com
This code works with gen.engine ? i thnik it must be gen.coroutine to work like this, without ge.Task.

Grato,

Waldecir

A. Jesse Jiryu Davis

unread,
Sep 3, 2013, 1:22:09 PM9/3/13
to python-...@googlegroups.com
Sorry, I meant gen.coroutine, not gen.engine.

Although a fun fact is that gen.engine actually does work if you yield a Future within it, since both engine and coroutine use the same Runner class to actually iterate the generator and handle the values it yields.

carl Cheng

unread,
Sep 3, 2013, 10:11:46 PM9/3/13
to python-...@googlegroups.com
Thanks!

so I think the following code is also OK?

class GenAsyncHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    @tornado.gen.engine
    def get(self):
        http_client = tornado.httpclient.AsyncHTTPClient()
        result0, result1 = yield [tornado.gen.Task(http_client.fetch,"http://example0.com"),\
                                  tornado.gen.Task(http_client.fetch,"http://example1.com")]
        self.finish('Fin.')
Reply all
Reply to author
Forward
0 new messages