tornode源码解析

单词:appliaction 应用, 申请

   handlers  操作者,处理程序

   transforms 变换

   default_host 默认主机

 

遇到问题1

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)
源码

测试

class a():
    def __init__(self):
        self.aaaa = []
        self.aaaa.append(b)
        print(self.aaaa)

class b():
    pass
obj = a()

#[<class '__main__.b'>]

执行append b之前 b必须先存着,import  tornado.ioloop 这个模块时,应该是把这个模块中的所有类都放内存了

__init__ 是后来调用时执行的,这时GZipContentEncoding类已经存在了

2、字典get

dict = {'Name': 'Zara', 'Age': 7}

print("Value : %s" %  dict.get('Age'))
print("Value : %s" %  dict.get('Sex', "aaa"))
View Code

get 找字典中key对于的value 找不到可以指定返回值

 

 

 

 

 

 

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

 

    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.handlers = []
        self.named_handlers = {}
        self.default_host = default_host
        self.settings = settings
        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"):
            path = self.settings["static_path"]
            handlers = list(handlers or [])
            static_url_prefix = settings.get("static_url_prefix",
                                             "/static/")
            static_handler_class = settings.get("static_handler_class",
                                                StaticFileHandler)
            static_handler_args = settings.get("static_handler_args", {})
            static_handler_args['path'] = path
            for pattern in [re.escape(static_url_prefix) + r"(.*)",
                            r"/(favicon\.ico)", r"/(robots\.txt)"]:
                handlers.insert(0, (pattern, static_handler_class,
                                    static_handler_args))
        if handlers:
            self.add_handlers(".*$", handlers)

        if self.settings.get('debug'):
            self.settings.setdefault('autoreload', True)
            self.settings.setdefault('compiled_template_cache', False)
            self.settings.setdefault('static_hash_cache', False)
            self.settings.setdefault('serve_traceback', True)

        # Automatically reload modified modules
        if self.settings.get('autoreload'):
            from tornado import autoreload
            autoreload.start()
__init__

 

代码执行过程:

(1)把 MainHandler 扔内存 

(2)执行Application 的"__ini__"t方法 就传了一个参数 handlers = [(r"/", MainHandler),]其余的都是默认参数

  self.transforms = []     settings ={} 

  self.handlers = []
  self.named_handlers = {}
  self.default_host = default_host
  self.settings = settings
  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", {}))

  

  

 

posted @ 2016-06-13 15:43  若时光搁浅  阅读(345)  评论(0)    收藏  举报