Code execution process and analysis:
- When we instantiate the application class in the main function, we pass in the parameter "Debug=true" (in Python, parameter types such as ' **settings ' allow incoming arguments with parameter names, which is actually incoming dictionary)
1App =Tornado.web.Application (2Handlers=[(R'/', Indexhandler)],3Template_path=os.path.join (Os.path.dirname (__file__),"Templates"),4Static_path=os.path.join (Os.path.dirname (__file__),"Static"),5debug=True6)
- At the same time that the application class is instantiated, its constructor is called, and the following code that is associated with the parameter "Debug=true" is posted for analysis:
1 ifSelf.settings.get ('Debug'):2Self.settings.setdefault ('Autoreload', True)3Self.settings.setdefault ('Compiled_template_cache', False)4Self.settings.setdefault ('Static_hash_cache', False)5Self.settings.setdefault ('Serve_traceback', True)
Because the incoming debug value is True, the Self.settings.get (' debug ') function statement is executed sequentially, at which point the value of ' Autoreload ' is set to true, and the value of ' Compiled_template_cache ' is false The value of ' Static_hash_cache ' is false, and the value of ' Serve_traceback ' is true.
- Autoreload Module Implementation Auto Restart Server autoreload is set to "True", the constructor of the application class continues with the following function:
1 # automatically reload modified modules 2 if self.settings.get ('autoreload'):3from Import autoreload4 Autoreload.start ()
At this point the server tries to restart
4.
In summary, when instantiating the application class, passing in the "debug=true" parameter will implement the dynamic interpretation of the code, Every time when the contents of the. py file are changed, Tornado automatically restarts the server, and we can see the new debugging effect when we refresh the page without having to turn on the server frequently.
Information reference: Http://www.tornadoweb.org/en/stable/web.html#tornado.web.Application
How the application class implements the Auto-reload function with debug parameters