阅读 Tornado 源码过程中的一个疑惑,求解答 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
kidlj
V2EX    Tornado

阅读 Tornado 源码过程中的一个疑惑,求解答

  •  
  •   kidlj 2016-01-05 19:50:34 +08:00 9776 次点击
    这是一个创建于 3645 天前的主题,其中的信息可能已经有所发展或是发生改变。

    下面是 tornado.httpclient.AsyncHTTPClient类的 fetch()方法的源代码。我没有在里面找到任何"fetch"的动作,它是怎么实现 “ Executes a request, asynchronously returning an HTTPResponse”的?

    完整代码在: https://github.com/tornadoweb/tornado/blob/master/tornado/httpclient.py

    def fetch(self, request, callback=None, raise_error=True, **kwargs): """Executes a request, asynchronously returning an `HTTPResponse`.  The request may be either a string URL or an `HTTPRequest` object.  If it is a string, we construct an `HTTPRequest` using any additional  kwargs: ``HTTPRequest(request, **kwargs)``  This method returns a `.Future` whose result is an  `HTTPResponse`. By default, the ``Future`` will raise an  `HTTPError` if the request returned a non-200 response code  (other errors may also be raised if the server could not be  contacted). Instead, if ``raise_error`` is set to False, the  response will always be returned regardless of the response  code.  If a ``callback`` is given, it will be invoked with the `HTTPResponse`.  In the callback interface, `HTTPError` is not automatically raised.  Instead, you must check the response's ``error`` attribute or  call its `~HTTPResponse.rethrow` method.  """ if self._closed: raise RuntimeError("fetch() called on closed AsyncHTTPClient") if not isinstance(request, HTTPRequest): request = HTTPRequest(url=request, **kwargs) else: if kwargs: raise ValueError("kwargs can't be used if request is an HTTPRequest object") # We may modify this (to add Host, Accept-Encoding, etc), # so make sure we don't modify the caller's object. This is also # where normal dicts get converted to HTTPHeaders objects. request.headers = httputil.HTTPHeaders(request.headers) request = _RequestProxy(request, self.defaults) future = TracebackFuture() if callback is not None: callback = stack_context.wrap(callback) def handle_future(future): exc = future.exception() if isinstance(exc, HTTPError) and exc.response is not None: response = exc.response elif exc is not None: response = HTTPResponse( request, 599, error=exc, request_time=time.time() - request.start_time) else: response = future.result() self.io_loop.add_callback(callback, response) future.add_done_callack(handle_future) def handle_response(response): if raise_error and response.error: future.set_exception(response.error) else: future.set_result(response) self.fetch_impl(request, handle_response) return future def fetch_impl(self, request, callback): raise NotImplementedError() 
    6 条回复    2016-01-06 23:50:35 +08:00
    phithon
        1
    phithon  
       2016-01-05 20:26:06 +08:00   1
    ![]( )
    impl 看名字应该是个接口,等着子类去实现的。
    你可以看 AsyncHTTPClient 的 `__new__` 方法,实际上执行 `new AsyncHTTPClient()` ,其返回的对象是一个 SimpleAsyncHTTPClient 实例, SimpleAsyncHTTPClient 是对 AsyncHTTPClient 的实现。
    所以,最后执行的 fetch_impl 方法,实际上在这里:
    https://github.com/tornadoweb/tornado/blob/6b312f7a73dc78de4b1be2a71a5bbfa51acc725d/tornado/simple_httpclient.py#L122
    kidlj
        2
    kidlj  
    OP
       2016-01-05 20:29:09 +08:00 via Android
    @phithon 非常感谢!
    sujin190
        3
    sujin190  
       2016-01-05 20:45:30 +08:00
    https://github.com/tornadoweb/tornado/blob/master/tornado/httpclient.py#L148
    这设置了生成实例时是哪个实现的实例
    sujin190
        4
    sujin190  
       2016-01-05 20:47:41 +08:00
    不过比较坑的是 SimpleAsyncHTTPClient 本身不支持连接池,每次都是打开新连接,除此之外还有一个实现是 CurlAsyncHTTPClient ,支持连接池,不过在 pypy 下似乎会出现栈溢出错误
    kidlj
        5
    kidlj  
    OP
       2016-01-05 21:33:08 +08:00 via Android
    @sujin190 谢谢。我的疑问得到了解答。
    mqingyn616
        6
    mqingyn616  
       2016-01-06 23:50:35 +08:00
    @sujin190 这个问题有一个比较靠谱的解决方案,可以看 github 上的这个 pr , https://github.com/tornadoweb/tornado/pull/1622

    给 SimpleAsyncHTTPClient 增加连接池,亲测可用。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5317 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 28ms UTC 08:50 PVG 16:50 LAX 00:50 JFK 03:50
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86