惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
腾讯CDC
博客园 - 司徒正美
A
About on SuperTechFans
H
Help Net Security
J
Java Code Geeks
C
Check Point Blog
B
Blog RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
MongoDB | Blog
MongoDB | Blog
U
Unit 42
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
MyScale Blog
MyScale Blog
V
Visual Studio Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
I
InfoQ
H
Hackread – Cybersecurity News, Data Breaches, AI and More
F
Fortinet All Blogs
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
GbyAI
GbyAI
博客园 - 【当耐特】
雷峰网
雷峰网

Tornado

docker 多进程部署 tornado Tornado 如何拿到传输的文件流 Tornado 的异步 怎么写的 tornado 下 ckeditor 图片上传问题 Tornado 异步怎么写的? tornado 怎么能确保端口挂了 服务也不会停止? Tornado 路由有什么好方法,一个一个写太麻烦了,有没有根据 url 和注解匹配的方法呢 Tornado V4.5 release pypy 拿到赞助之后出了支持 Python3.5 的版本 请教 tornado 文件上传问题 tornado 做后台服务,如果前端没有用 cookie. tornado 优秀项目推荐 问一个 tornado 解决阻塞的方案问题? tornaqiniu(基于 tornado 的异步七牛 sdk) tornado 并行异步,如何保证在只有部分请求成功后,结果依旧可用。 nginx 反向代理 tornado,如何 location 首页u rl? 问一个关于 tornado 异常处理的新手问题 tornado 的 coroutine 能和自定义的装饰器一起使用吗?? Tornado 并发数很低,正常么? gtornado - tornado 中 pymysql, pymemcache, storm orm 支持库 Tornado 如何组织中大型项目,你们都是怎么样做的? tornado 跑不通,小白求帮忙 Tornado 4.3 发布 Tornado 官方怎么不好好维护一个 sql 的异步数据库驱动? 关于 tornado 阻塞的问题 tornado 的 mysql 异步驱动性能测试 pypy 之 tornado tornado 多个 url RequestHandler 类的 get_current_user() 方法覆写无效 tornado 动态添加 url 的问题
阅读 Tornado 源码过程中的一个疑惑,求解答
kidlj · 2016-01-05 · via Tornado

下面是 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_callback(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()