tornado AsyncHTTPClient dynamic add http_client.fetch to list

163 views Asked by At

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.

1

There are 1 answers

0
kwarunek On BEST ANSWER

Using simple `for, additional list will be needed

ids = [111,222,333,444]
futures = []
for iid in ids:
    futures.append(http_client.fetch(url + iid)
responses = yield futures

# prepare output to user
bodies = []
for r in responses:
    bodies.append(r.body)

# join list of response bodies 
output = ' '.join(bodies) 
self.write(output)

Or you can use list comprehension

class GetdataHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    @tornado.gen.engine
    def get(self):
        http_client = tornado.httpclient.AsyncHTTPClient()
        url = 'http://u.com/order/fa-huo?id={}'

        ids = [111,222,333,444]  
        # from GET/POST params 
        # ids = self.get_arguments('ids')

        responses = yield [
            http_client.fetch(url.format(iid)) for iid in ids
        ]

        output = ' '.join(
            ['getData: {}'.format(r.body) for r in responses]
        )
        self.write(output)   
        self.finish()

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 of raise_exception argument in fetch).

More information: