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

推荐订阅源

D
Docker
B
Blog RSS Feed
Microsoft Security Blog
Microsoft Security Blog
Y
Y Combinator Blog
N
Netflix TechBlog - Medium
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Check Point Blog
The GitHub Blog
The GitHub Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
GbyAI
GbyAI
博客园_首页
A
About on SuperTechFans
Blog — PlanetScale
Blog — PlanetScale
人人都是产品经理
人人都是产品经理
T
Tailwind CSS Blog
aimingoo的专栏
aimingoo的专栏
T
The Blog of Author Tim Ferriss
The Cloudflare Blog

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