【问题标题】:tornado AsyncHTTPClient dynamic add http_client.fetch to listtornado AsyncHTTPClient 动态添加 http_client.fetch 到列表
【发布时间】:2017-01-06 09:49:54
【问题描述】:

这里是代码

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()

这是一种愚蠢的方式。 我将从 mysql 获取 id。我不知道我需要请求多少个 id。 我不知道如何使用(for)来做到这一点。 不胜感激。

【问题讨论】:

    标签: python mysql list tornado asynchttpclient


    【解决方案1】:

    使用简单的`for,需要额外的列表

    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)
    

    或者你可以使用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()
    

    但请注意,缺少错误处理。例如 - 如果fetch 之一引发异常,您将不会得到任何结果,您应该封装一些带有错误处理的函数(可能使用raise_exception argument in fetch)。

    更多信息:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-21
      • 2019-04-22
      • 2016-11-17
      相关资源
      最近更新 更多