从tornado的 Hello,world 开始分析tornado的源码
python
import tornado.ioloop import tornado.web class MainHandler(tornado.web.RequestHandler): def get(self): self.write(\"Hello, world\") application = tornado.web.Application([ (r\"/\", MainHandler), ]) if __name__ == \"__main__\": application.listen(8888) tornado.ioloop.IOLoop.instance().start()
很容易可以看出,通过继承RequestHandler
类定义自己的处理类,来处理请求。Application类的对象来处理URI的路由(将URIr\"/\"
于处理类MainHandler
组成tuple,关联起来)。
__init__
简化版代码:
python
def __init__(self, handlers=None, default_host=\"\", transforms=None, **settings): if transforms is None: self.transforms = [] if settings.get(\"compress_response\") or settings.get(\"gzip\"): self.transforms.append(GZipContentEncoding) else: self.transforms = transforms ...... self.ui_modules = {\'linkify\': _linkify, \'xsrf_form_html\': _xsrf_form_html, \'Template\': TemplateModule, } self.ui_methods = {} self._load_ui_modules(settings.get(\"ui_modules\", {})) self._load_ui_methods(settings.get(\"ui_methods\", {})) if self.settings.get(\"static_path\"): ...... if handlers: self.add_handlers(\".*$\", handlers) if self.settings.get(\'debug\'): self.settings.setdefault(\'autoreload\', True) ...... # Automatically reload modified modules if self.settings.get(\'autoreload\'): from tornado import autoreload autoreload.start()
参数handlers
是一个list,list里每个object是一个URLSpec
的对象或tuple。tuple可以是二到四个element,分别是URI的正则、handler类、用于初始化URLSpec
的kwargs、handler的name。
(下面add_handlers详细说明)
参数settings
是一个dict,所有settings的具体用法
add_handlers
方法添加handlers。add_handle
python
def add_handlers(self, host_pattern, host_handlers): if not host_pattern.endswith(\"$\"): host_pattern += \"$\" handlers = [] if self.handlers and self.handlers[-1][0].pattern == \'.*$\': self.handlers.insert(-1, (re.compile(host_pattern), handlers)) else: self.handlers.append((re.compile(host_pattern), handlers)) for spec in host_handlers: if isinstance(spec, (tuple, list)): assert len(spec) in (2, 3, 4) spec = URLSpec(*spec) handlers.append(spec) if spec.name: if spec.name in self.named_handlers: app_log.warning( \"Multiple handlers named %s; replacing previous value\", spec.name) self.named_handlers[spec.name] = spec
将host_pattern和handlers,组成tuple加到self.handlers
的末尾但是在匹配所有域名的tuple前。
由spec = URLSpec(*spec)
易看出初始化Application的时候的第一个参数存的tuple是用来初始化URLSpec的所以参数顺序应该和URLSpec要求的一样(def __init__(self, pattern, handler, kwargs=None, name=None)
)。
用过第四个参数name来构造反响代理,储存在Application的named_handlers(dict)里。
hello world里调用了Application的listen
和tornado.ioloop.IOLoop.instance().start()
(以后会详细介绍ioloop),来真正启动。
listen
python
def listen(self, port, address=\"\", **kwargs): from tornado.httpserver import HTTPServer server = HTTPServer(self, **kwargs) server.listen(port, address)
实例化一个HTTPServer,将application绑定上去。HTTPServer调用application的start_request
来将application和connection绑定在一起初始化一个_RequestDispatcher的对象,由其来处理请求的路由,来利用add_handler
建立的规则。
原文连接
上一篇:setup.py 技巧