here is the code
class GetdataHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
@tornado.gen.engine
def get(self):
http_client = tornado.httpclient.AsyncHTTPClient()
response1, response2, response3, response4, response5, response6, response7, response8, response9, response10 = yield [
http_client.fetch('http://u.com/order/fa-huo?id=85223'),
http_client.fetch('http://u.com/order/fa-huo?id=85224'),
http_client.fetch('http://u.com/order/fa-huo?id=85225'),
http_client.fetch('http://u.com/order/fa-huo?id=85226'),
http_client.fetch('http://u.com/order/fa-huo?id=85227'),
http_client.fetch('http://u.com/order/fa-huo?id=85228'),
http_client.fetch('http://u.com/order/fa-huo?id=85229'),
http_client.fetch('http://u.com/order/fa-huo?id=85230'),
http_client.fetch('http://u.com/order/fa-huo?id=85231'),
http_client.fetch('http://u.com/order/fa-huo?id=85232')]
self.write('getData: %s getData: %s getData: %s getData: %s getData: %s getData: %s getData: %s getData: %s getData: %s getData: %s'%(response1.body,response2.body,response3.body,response4.body,response5.body,response6.body,response7.body,response8.body,response9.body,response10.body))
self.finish()
this's a stupid way. I'll get ids from mysql.I don't know how many ids I need to request. I don't know how to do it with a (for). Would appreciate any advice.
Using simple `for, additional list will be needed
Or you can use list comprehension
But note that there is missing error handling. For example - if one of the
fetch
raises exception you won't get any results, you should wrap in some function with error handling (maybe use ofraise_exception
argument infetch
).More information:
python list comprehension explained
https://emptysqua.re/blog/multiple-asynchronous-events-tornado/