Tornado web.py Application class source analysis

Source: Internet
Author: User
Tags stack trace

"Course" web2.0 programming
"Job Requirements" Study Application object source code. When the Application object is instantiated, the "debug=true" parameter is given, and the code is dynamically compiled automatically.
"Reference Document" application class source code Tornado application Official document debug mode and automatic reload

Tornado.web provides the asynchronous functionality of a simple web framework . A collection of request handlers makes up a Web application.

1. Analysis of the source code of the application class we know that the application class has the following methods:

· __init__ (self, handlers=none, default_host= "" transforms =none, **settings)

< Span class= "PL-VPF" > constructor, accepts a Urlspec class object or a tuple of (regular expression, requested Class) as a parameter. When we accept the request, the program traverses the list sequentially and instantiates the request class that the first regular expression can match.
/span>

< Span class= "PL-VPF" >

< Span class= "PL-VPF" >

< Span class= "PL-VPF" > General tornado in setting have: Autoreload , debug,default_handler_class , compress_response , gzip, serve_traceback , Log_function, ui_modules , ui_method

< Span class= "PL-VPF" > authentication and security settings setting: cookie_secret , login_url , xsrf_cookies , < Span class= "Pre" >xsrf_cookie_version and so on.

< Span class= "PL-VPF" > template set setting: autoescape,COMPILED_TEMPLATE_CACHE,template_path , Template_loader

< Span class= "PL-VPF" > Static file Settings setting:static_hash_cache , static_path, STATIC_URL_PREFIX,static_handler_class , static_handler_args

· Listen (self, port, address="", * *Kwargs)

For this application, start the HTTP server on the given port. This is an easy way to create a Httpserver object and invoke the Listen function.

Note: Httpserver.listen Unsupported keyword parameters are passed through the constructor of Httpserver; In advanced uses such as multi-process mode, instead of calling this method, a Httpserver object is created and called directly Tcpserver.bind /tcpserver.start (); After calling this function, you still need to call Ioloop.instance (). Start () to begin the service.

· _get_host_handlers (self, request)

Gets the host's handlers, if not found, returns false;

The following functions correspond to some of the values of setting in the constructor:

· _load_ui_methods (Self, methods)

· _load_ui_modules (self, modules)

· start_request (self, connection)

· __call__ (self, request)
A syntax feature of Python that allows application to be called directly as a function

· Reverse_url (self, name, *args)

Return a handler URL path, this handler must have been added as Urlspec application inside,

· log_request (self, handler)
A complete log of HTTP requests that is written to root logger by default. If you want to change the location of logs storage, you can override this method or pass a function in application's setting dictionary as Log_function

2. Now we focus on the analysis of Application object instantiation, give the "debug=true" parameter, code dynamic automatic compilation principle, mainly is the constructor parameter list setting value.

If you pass debug=true as a parameter into the application constructor, the application will run in debug mode. In this mode, there are a few features that are set up, and these features can be set individually, and when both settings are present, the settings are prioritized higher than the default settings.

Features are as follows:

Autoreload=true: When there is any change , the application monitors its source file for changes and reloads itself. This reduces the development process by manually restarting the server. However, some failures, such as syntax errors at import, are currently not directly recoverable by debug mode.
Compiled_template_cache=false: Templates are not cached.
Static_hash_cache=false: The hash value of the static file (the Static_url function will be used) will not be cached
Serve_traceback=true ': When an RequestHandler exception is not captured, an error page is generated, including a stack trace.

Automatic reload mode is incompatible with multithreaded mode. The automatic loading feature of the debug mode can be used as a standalone module of the tornado.autoreload, which can be combined with: set Autoreload=true to detect changes in the operation of the application and Python-m Tornado.autoreload Myserver.py method to open an application to catch an asynchronous exception or other error.

Overloading loses the command parameters of the Python interpreter because his re-execution uses sys.executable and SYS.ARGV, so modifying these values causes the overloads to be incorrect. On some platforms, the process cannot be updated in the appropriate location, then the changes to the code are detected, the old service is still in place, and the new service has begun, causing confusion for the IDE.

Tornado web.py Application class source analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.