tornado.autoreload — Automatically detect code changes in development

A module to automatically restart the server when a module is modified.

Most applications should not call this module directly. Instead, pass the keyword argument debug=True to the tornado.web.Application constructor. This will enable autoreload mode as well as checking for changes to templates and static resources.

This module depends on IOLoop, so it will not work in WSGI applications and Google AppEngine. It also will not work correctly when HTTPServer’s multi-process mode is used.

Reloading loses any Python interpreter command-line arguments (e.g. -u) because it re-executes Python using sys.executable and sys.argv. Additionally, modifying these variables will cause reloading to behave incorrectly.

tornado.autoreload.start(io_loop=None, check_time=500)[source]

Restarts the process automatically when a module is modified.

We run on the I/O loop, and restarting is a destructive operation, so will terminate any pending requests.

tornado.autoreload.wait()[source]

Wait for a watched file to change, then restart the process.

Intended to be used at the end of scripts like unit test runners, to run the tests again after any source file changes (but see also the command-line interface in main)

tornado.autoreload.watch(filename)[source]

Add a file to the watch list.

All imported modules are watched by default.

tornado.autoreload.add_reload_hook(fn)[source]

Add a function to be called before reloading the process.

Note that for open file and socket handles it is generally preferable to set the FD_CLOEXEC flag (using fcntl or tornado.platform.auto.set_close_exec) instead of using a reload hook to close them.

tornado.autoreload.main()[source]

Command-line wrapper to re-run a script whenever its source changes.

Scripts may be specified by filename or module name:

python -m tornado.autoreload -m tornado.test.runtests
python -m tornado.autoreload tornado/test/runtests.py

Running a script with this wrapper is similar to calling tornado.autoreload.wait at the end of the script, but this wrapper can catch import-time problems like syntax errors that would otherwise prevent the script from reaching its call to wait.

Previous topic

Utilities

Next topic

tornado.gen — Simplify asynchronous code

This Page