tornado模板源码小析

最近对flask的热情有点下降,对tornado有点高涨。 之前在知乎上回答过一个问题,如何理解 Tornado ?,我的回答如下:

1.高性能的网络库,这可以和gevent,twisted,libevent等做对。

提供了异步io支持,超时事件处理,在此基础上提供了tcpserver,httpclient,尤其是curlhttpclient,

在现有http客户端中肯定排第一。可以用来做爬虫,游戏服务器,据我所知业界已有使用tornado作为游戏服务器

2.web框架,这可以和django,flask对。

提供了路由,模板等web框架必备组件。与其他区别是tornado是异步的,天然适合长轮训,

这也是friendfeed发明tornado的原因,当前flask也可以支持,但必须借助gevent等

3.较为完备的http服务器,这点可以和nginx,apache对比,

但只支持http1.0,所以使用nginx做前段不仅是为了更好利用多核,也是让其支持http1.1

4.完备的wsgi服务器,这可以和gunicore,gevent wsgi server做对比,

也就是说可以让flask运行在tornado之上,让tornado加速flask

5.提供了完备的websocket支持,这让html5的游戏等提供了便利。

像知乎长轮训就是使用了websocket,但websocket手机支持的不是很好,

前段时间不得不使用定时ajax发送大量请求,期待手机浏览器赶快奋起直追

最近研究了下tornado的模板,实现的比较简洁,在这里总结一下。

tornado的模板基本都在template.py这个文件中,短短800多行代码就实现了基本可用的模板,让我们慢慢揭开她的面纱。

首先我们看看tornado是如何编译模板的,下面是个简单的模板

t = Template("""{%if names%}
    {% for name in names %}
        {{name}}
    {%end%}
{%else%}
no one
{%end%}
""")

tornado最后编译代码如下:

def _tt_execute():  # <string>:0
    _tt_buffer = []  # <string>:0
    _tt_append = _tt_buffer.append  # <string>:0
    if names:  # <string>:1
        _tt_append(‘\n    ‘)  # <string>:2
        for name in names:  # <string>:2
            _tt_append(‘\n        ‘)  # <string>:3
            _tt_tmp = name  # <string>:3
            if isinstance(_tt_tmp, _tt_string_types): _tt_tmp = _tt_utf8(_tt_tmp)  # <string>:3
            else: _tt_tmp = _tt_utf8(str(_tt_tmp))  # <string>:3
            _tt_tmp = _tt_utf8(xhtml_escape(_tt_tmp))  # <string>:3
            _tt_append(_tt_tmp)  # <string>:3
            _tt_append(‘\n    ‘)  # <string>:4
            pass  # <string>:2
        _tt_append(‘\n‘)  # <string>:5
        pass  # <string>:5
    else:  # <string>:5
        _tt_append(‘\nno one\n‘)  # <string>:7
        pass  # <string>:1
    _tt_append(‘\n‘)  # <string>:8
    return _tt_utf8(‘‘).join(_tt_buffer)  # <string>:0

是的,你没看错,tornado编译就是将之翻译成一个个代码块,最后通exec传递我们给的参数命名空间执行_tt_execute函数。

在我们上面的模板中包含了4种预定义的NODE节点,_ControlBlock,_Expression,_TEXT,每种Node节点都有自己的生成方式。

比如说_Expression表达式节点,也就是我们模板中的{{name}},当_parse解析时发现‘{‘后面还是‘{‘就认为是表达式节点,

class _Expression(_Node):
    def __init__(self, expression, line, raw=False):
        self.expression = expression
        self.line = line
        self.raw = raw

    def generate(self, writer):
        writer.write_line("_tt_tmp = %s" % self.expression, self.line)
        writer.write_line("if isinstance(_tt_tmp, _tt_string_types):"
                          " _tt_tmp = _tt_utf8(_tt_tmp)", self.line)
        writer.write_line("else: _tt_tmp = _tt_utf8(str(_tt_tmp))", self.line)
        if not self.raw and writer.current_template.autoescape is not None:
            # In python3 functions like xhtml_escape return unicode,
            # so we have to convert to utf8 again.
            writer.write_line("_tt_tmp = _tt_utf8(%s(_tt_tmp))" %
                              writer.current_template.autoescape, self.line)
        writer.write_line("_tt_append(_tt_tmp)", self.line)

最后生成时会调用节点的generate方法,self.expression就是上面的name,所以当exec的时候就会把name的值append到内部的列表中。
像if,for等都是控制节点,他们的定义如下:

class _ControlBlock(_Node):
    def __init__(self, statement, line, body=None):
        self.statement = statement
        self.line = line
        self.body = body

    def each_child(self):
        return (self.body,)

    def generate(self, writer):
        writer.write_line("%s:" % self.statement, self.line)
        with writer.indent():
            self.body.generate(writer)
            # Just in case the body was empty
            writer.write_line("pass", self.line)

控制节点的generate方法有点意义,因为if,for等是下一行是需要缩进的,所以调用了with writer.indent继续缩进控制,可以看下

_CodeWriter的indent方法。

节点中比较有意思的是_ExtendsBlock,这是实现目标基础的节点,

class _ExtendsBlock(_Node):
    def __init__(self, name):
        self.name = name

我们发现并没有定义generate方法,那当生成继承节点时不是会报错吗?让我们看一段事例

loader = Loader(‘.‘)
t=Template("""{% extends base.html %}
{% block login_name %}hello world! {{ name }}{% end %}
""",loader=loader)

当前目录下base.html如下:

<html>
<head>
<title>{{ title }}</title>
</head>
<body>
{% block login_name %}hello! {{ name }}{% end %}
</body>
</html> 

我们可以看看解析后的节点,

由于我们继承了base.html,所以我们的应该以base.html的模板生成,并使用新定义的block代替base.html中的block,
这是很正常的思路,tornado也的确是这么干的,只不过处理的并不是在_ExtendsBlock。

而实在Template的_generate_python中

   def _generate_python(self, loader, compress_whitespace):
        buffer = StringIO()
        try:
            # named_blocks maps from names to _NamedBlock objects
            named_blocks = {}
            ancestors = self._get_ancestors(loader)
            ancestors.reverse()
            for ancestor in ancestors:
                ancestor.find_named_blocks(loader, named_blocks)
            writer = _CodeWriter(buffer, named_blocks, loader, ancestors[0].template,
                                 compress_whitespace)
            ancestors[0].generate(writer)
            return buffer.getvalue()
        finally:
            buffer.close()

    def _get_ancestors(self, loader):
        ancestors = [self.file]
        for chunk in self.file.body.chunks:
            if isinstance(chunk, _ExtendsBlock):
                if not loader:
                    raise ParseError("{% extends %} block found, but no "
                                     "template loader")
                template = loader.load(chunk.name, self.name)
                ancestors.extend(template._get_ancestors(loader))
        return ancestors

_generate_python中调用_get_ancestors获取当前模板的父模板,我们看到如果当前模板的_FILE节点中有_ExtendsBlock就代表有父模板并通过loader.load加载父模板,此时父模板已经是解析过的_FILE节点了。所以,在上面的模板中,ancestors是[当前模板_FILE节点,父模板_FILE节点],ancestors.reverse()后其实ancestors[0]就是父模板,我们看到最后是通过ancestors[0].generate(writer)来生成代码的。那当前模板是如何替换父模板的block内容呢?

看上图,block login_name通过解析为_NamedBlock,在_generate_python中通过调用ancestor.find_named_blocks来替换

父模板的_NamedBlock的。

for ancestor in ancestors:
       ancestor.find_named_blocks(loader, named_blocks)

ancestor其实就是_FILE节点,find_named_blocks将遍历_FILE节点中所有节点并调用find_named_blocks

class _NamedBlock(_Node):
    def find_named_blocks(self, loader, named_blocks):
        named_blocks[self.name] = self
        _Node.find_named_blocks(self, loader, named_blocks)

其它节点find_named_blocks都没有做什么事,_NamedBlock通过named_blocks[self.name] = self替换为当前模板的_NamedBlock,因为ancestors父模板在前,当前模板在后,所以最后使用的是当前模板的_NamedBlock。

生成代码后generate将在给定的命名空间中exec代码

    def generate(self, **kwargs):
        """Generate this template with the given arguments."""
        namespace = {
            "escape": escape.xhtml_escape,
            "xhtml_escape": escape.xhtml_escape,
            "url_escape": escape.url_escape,
            "json_encode": escape.json_encode,
            "squeeze": escape.squeeze,
            "linkify": escape.linkify,
            "datetime": datetime,
            "_tt_utf8": escape.utf8,  # for internal use
            "_tt_string_types": (unicode_type, bytes_type),
            # __name__ and __loader__ allow the traceback mechanism to find
            # the generated source code.
            "__name__": self.name.replace(‘.‘, ‘_‘),
            "__loader__": ObjectDict(get_source=lambda name: self.code),
        }
        namespace.update(self.namespace)
        namespace.update(kwargs)
        exec_in(self.compiled, namespace)
        execute = namespace["_tt_execute"]
        # Clear the traceback module‘s cache of source data now that
        # we‘ve generated a new template (mainly for this module‘s
        # unittests, where different tests reuse the same name).
        linecache.clearcache()
        return execute()

所以在模板中可以使用datetime等,都是通过在这里注入到模板中的,当然还有其它的是通过

web.py 中get_template_namespace注入的

   def get_template_namespace(self):
        """Returns a dictionary to be used as the default template namespace.

        May be overridden by subclasses to add or modify values.

        The results of this method will be combined with additional
        defaults in the `tornado.template` module and keyword arguments
        to `render` or `render_string`.
        """
        namespace = dict(
            handler=self,
            request=self.request,
            current_user=self.current_user,
            locale=self.locale,
            _=self.locale.translate,
            static_url=self.static_url,
            xsrf_form_html=self.xsrf_form_html,
            reverse_url=self.reverse_url
        )
        namespace.update(self.ui)
        return namespace

我们再来看看tornado的模板是如何对UI模块的支持的。

{% for entry in entries %}
  {% module Entry(entry) %}
{% end %}

在使用module时将会生成_Module节点

class _Module(_Expression):
    def __init__(self, expression, line):
        super(_Module, self).__init__("_tt_modules." + expression, line,
                                      raw=True)

我们看到其实_Module节点是继承自_Expression节点,所以最后执行的是_tt_modules.Entry(entry)

_tt_modules定义在web.py的RequestHandler中

self.ui["_tt_modules"] = _UIModuleNamespace(self,application.ui_modules)

并通过上文的get_template_namespace中注入到模板中。

class _UIModuleNamespace(object):
    """Lazy namespace which creates UIModule proxies bound to a handler."""
    def __init__(self, handler, ui_modules):
        self.handler = handler
        self.ui_modules = ui_modules

    def __getitem__(self, key):
        return self.handler._ui_module(key, self.ui_modules[key])

    def __getattr__(self, key):
        try:
            return self[key]
        except KeyError as e:
            raise AttributeError(str(e))

所以当执行_tt_modules.Entry(entry)时先访问_UIModuleNamespace的__getattr__,后访问__getitem__,最后调用

handler._ui_module(key, self.ui_modules[key]),

    def _ui_module(self, name, module):
        def render(*args, **kwargs):
            if not hasattr(self, "_active_modules"):
                self._active_modules = {}
            if name not in self._active_modules:
                self._active_modules[name] = module(self)
            rendered = self._active_modules[name].render(*args, **kwargs)
            return rendered
        return render

_tt_modules.Entry(entry)中entry将会传给_ui_module内部的render,也就是args=entry

self._active_modules[name] = module(self)此时就是实例化后的UIModule,调用render获取渲染后的内容

class Entry(tornado.web.UIModule):
    def render(self, entry, show_comments=False):
        return self.render_string(
            "module-entry.html", entry=entry, show_comments=show_comments)

当然如果你觉得这么做费事,也可以使用tornado自带的TemplateModule,它继承自UIModule,

你可以这么用

{% module Template("module-entry.html", show_comments=True) %}

在module_entry.html中可以通过set_resources引用需要的静态文件

{{ set_resources(embedded_css=".entry { margin-bottom: 1em; }") }}

这里需要注意的是:只能在Template引用的html文件中使用set_resources函数,因为set_resources是TemplateModule.render的内部函数

class TemplateModule(UIModule):
    """UIModule that simply renders the given template.

    {% module Template("foo.html") %} is similar to {% include "foo.html" %},
    but the module version gets its own namespace (with kwargs passed to
    Template()) instead of inheriting the outer template‘s namespace.

    Templates rendered through this module also get access to UIModule‘s
    automatic javascript/css features.  Simply call set_resources
    inside the template and give it keyword arguments corresponding to
    the methods on UIModule: {{ set_resources(js_files=static_url("my.js")) }}
    Note that these resources are output once per template file, not once
    per instantiation of the template, so they must not depend on
    any arguments to the template.
    """
    def __init__(self, handler):
        super(TemplateModule, self).__init__(handler)
        # keep resources in both a list and a dict to preserve order
        self._resource_list = []
        self._resource_dict = {}

    def render(self, path, **kwargs):
        def set_resources(**kwargs):
            if path not in self._resource_dict:
                self._resource_list.append(kwargs)
                self._resource_dict[path] = kwargs
            else:
                if self._resource_dict[path] != kwargs:
                    raise ValueError("set_resources called with different "
                                     "resources for the same template")
            return ""
        return self.render_string(path, set_resources=set_resources,
                                  **kwargs)
时间: 2024-07-28 18:45:11

tornado模板源码小析的相关文章

MVVM大比拼之vue.js源码精析

VUE 源码分析 简介 Vue 是 MVVM 框架中的新贵,如果我没记错的话作者应该毕业不久,现在在google.vue 如作者自己所说,在api设计上受到了很多来自knockout.angularjs等大牌框架影响,但作者相信 vue 在性能.易用性方面是有优势.同时也自己做了和其它框架的性能对比,在这里.今天以版本 0.10.4 为准 入口 Vue 的入口也很直白: ? 1 var demo = new Vue({ el: '#demo', data: { message: 'Hello V

SpringMVC学习——概念、流程图、源码简析(一)

学习资料:开涛的<跟我学SpringMVC.pdf> 众所周知,springMVC是比较常用的web框架,通常整合spring使用.这里抛开spring,单纯的对springMVC做一下总结. 概念 HandlerMapping:处理器映射,对请求的URL进行映射为具体的处理器(如果有拦截器也包含拦截器,会将Handler和多个HandlerInterceptor封装为HandlerExecutionChain对象) HandlerAdapter:处理器适配器,适配不同类型的处理器,如Cont

MVVM大比拼之AngularJS源码精析

MVVM大比拼之AngularJS源码精析 简介 AngularJS的学习资源已经非常非常多了,AngularJS基础请直接看官网文档.这里推荐几个深度学习的资料: AngularJS学习笔记 作者:邹业盛 .这个笔记非常细致,记录了作者对于AngularJS各个方面的思考,其中也不乏源码级的分析. 构建自己的AngularJS .虽然放出第一章后作者就写书去了.但这第一部分已经足以带领读者深入窥探angularJS在核心概念上的实现,特别是dirty check.有愿意继续深入的读者可以去买书

利用QJM实现HDFS自动主从切换(HA Automatic Failover)源码详析

最近研究了下NameNode HA Automatic Failover方面的东西,当Active NN因为异常或其他原因不能正常提供服务时,处于Standby状态的NN就可以自动切换为Active状态,从而到达真正的高可用 NN HA Automatic Failover架构图 为了实现自动切换,需要依赖ZooKeeper和ZKFC组件,ZooKeeper主要用来记录NN的相关状态信息,zkfc组件以单独的JVM进程的形式运行在NN所在的节点上.下面首先分析下NN的启动流程,NN对象在实例化过

ViewAnimator实例源码小Demo+Tab例子

ViewAnimator实例源码小Demo+Tab例子,仅供学习~ 例子中主要有ImageSwitcher.TextSwitcher.ViewFilpper.Tabs(ActionBar)的使用 源码下载地址:http://yunpan.cn/QacbksIx2Snme (提取码:acc0) ViewAnimator实例源码小Demo+Tab例子

程序站分类目录帝国cms模板源码分享

程序站网址导航基于帝国cms程序搭建,为互联网用户提供音乐.小说.NBA.财经.购物.视频.软件及热门游戏网址大全等,提供了多种搜索引擎入口.实用查询.天气预报.个性定制等各种分类的优秀内容和网站入口,提供简单便捷的上网导航服务. 本模板源码安全可靠,代码简单/开源,无绑定域名等限制.您可以使用本源码轻松建立自己的综合网址导航站点,也可以修改为您所喜爱的相关内容导航站.网站导航页面的系统模板源码,内核绿色风格,界面清爽自然首页顶部可以添加广告信息. 程序版本:EmpireCMS v7.2 Fre

30s源码刨析系列之函数篇

前言 由浅入深.逐个击破 30SecondsOfCode 中函数系列所有源码片段,带你领略源码之美. 本系列是对名库 30SecondsOfCode 的深入刨析. 本篇是其中的函数篇,可以在极短的时间内培养你的函数式思维. 内容根据源码的难易等级进行排版,目录如下: 新手级 普通级 专家级 正文 新手级 checkProp const checkProp = (predicate, prop) => obj => !!predicate(obj[prop]); const lengthIs4

并发工具-CyclicBarrier源码简析

CyclicBarrier是循环栅栏的意思,循环的等待多个线程执行任务: <1> 示例代码如下: public class CyclicBarrierTest { public static CyclicBarrier cb = new CyclicBarrier(3, () -> System.out.println("-------开始点名-------")); public static void main(String[] args) { System.out

tornado框架源码分析---Application类之debug参数

先贴上Application这个类的源码. class Application(httputil.HTTPServerConnectionDelegate): """A collection of request handlers that make up a web application. Instances of this class are callable and can be passed directly to HTTPServer to serve the a