Snake games-using Tornado to build a high-performance Web 2-autoreload

Source: Internet
Author: User

In the first part of this series, we created a helloword application and can access it through a browser. In this way, we can use Tornado to develop websites. Well, this is a good first step, but when you decide to use Tornado to develop a website and start writing new handler, you will find that the website needs to be frequently switched off and then restart the server, this is a crazy thing. We needProgramAfter modification, the service can automatically reload the newCode, The same as Asp.net. However, Tornado documentation is limited and no instructions can be found in this document.

However, when the mountains and waters are exhausted, there is no way to go.Source codeAn autoreload. py file is found in

I guess tornado can reload it by itself, but it is not described in the document, but how can I use autoreload. Open this file and you can see that it is actually very simple. There are only two functions, as shown in figure

The second function is private, so the start function is actually the only entry. We found in the code that the _ reload_on_update function is actually registered in the io_loop as a callback function in start, in fact, we only need to pass the ioloop into start to implement autoreload. Therefore

Def main ():
Tornado. Options. parse_command_line ()
Application = tornado. Web. Application ([
(R "/", mainhandler ),
])
Http_server = tornado. httpserver. httpserver (Application)
Http_server.listen (options. Port)
Tornado. ioloop. ioloop. instance (). Start ()

 

The code is changed:

Def main ():
Tornado. Options. parse_command_line ()
Application = tornado. Web. Application ([
(R "/", mainhandler ),
])
Http_server = tornado. httpserver. httpserver (Application)
Http_server.listen (options. Port)
Loop = tornado. ioloop. ioloop. instance ()
Tornado. autoreload. Start (loop)
Loop. Start ()

That's all.

Note: The reload here only detects changes to the py file. Any changes to other files, such as CSS and images, will not trigger reload.

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.