How to make tornado server auto restart / reload everytime files changes

How to make tornado server auto restart / reload everytime files changes

When running Tornado web server, we must doing restart for every changes that has been maded. This tedious works for sure. Are you newbie? Yes, I just download Tornado just today and I’m a new comers for this web-framework. If you follow Tornado documentation, it only showing how to running webserver in non-autoreload way. Wait, did you mean Tornado actually have module or function to make it autoreload everytime changes has been made? Yes!

Here is example how to using tornado.autoreload to reload server everytime changes has been made. It’s also “update” if files in templates (HTML) being changed as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import tornado.autoreload

from sqlalchemy.orm import scoped_session, sessionmaker
from models import engine
import logging
from urls import URLS

tornado.options.define("port", default=8000, help="Run on port", type=int)
tornado.options.define("environment", default="dev", help="environment")


class Application(tornado.web.Application):
    def __init__(self):
        debug = (tornado.options.options.environment == "dev")

        # Set settings
        settings = dict(
            static_path = os.path.join(os.path.dirname(__file__), "static"),
            template_path = os.path.join(os.path.dirname(__file__),
                                         "templates"),
            xsrf_cookies = True,
            autoescape = None,
            session_age = 7 * 24 * 60 * 60,
        )

        # Watch templates
        for (path, dirs, files) in os.walk(settings["template_path"]):
            for item in files:
                tornado.autoreload.watch(os.path.join(path, item))

        # Set URL handlers
        handlers = URLS
        tornado.web.Application.__init__(self, handlers, **settings)

        # global database connection
        self.db = scoped_session(sessionmaker(bind=engine))


def main():
    tornado.options.parse_command_line()
    logging.info("Starting tornado server on 0.0.0.0:%d" %
                 tornado.options.options.port)
    server = tornado.httpserver.HTTPServer(request_callback=Application(),
                                           xheaders=True)
    server.listen(tornado.options.options.port)
    io_loop = tornado.ioloop.IOLoop.instance()
    tornado.autoreload.start(io_loop)
    io_loop.start()

if __name__ == "__main__":
    main()

The key here located in this parts:

1. Watch on all files inside templates

1
2
3
4
# Watch templates
        for (path, dirs, files) in os.walk(settings["template_path"]):
            for item in files:
                tornado.autoreload.watch(os.path.join(path, item))

2. IOLoop using autoreload

1
2
3
io_loop = tornado.ioloop.IOLoop.instance()
tornado.autoreload.start(io_loop)
io_loop.start()

Now, when you’re running your server, everytime changes happen, it’s will restart automatically.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.