When developing Tornado applications, one common pain point is manually restarting the server after every code change. While researching online, many developers encounter partial solutions or outdated approaches to enabling debug mode.
The correct way to enable debug mode in Tornado involves setting multiple parameters in your application settings:
application = tornado.web.Application([
(r"/", MainHandler),
],
debug=True,
autoreload=True,
compiled_template_cache=False,
static_hash_cache=False,
serve_traceback=True
)
Each setting serves a specific purpose in development:
- debug=True: Enables debug mode globally
- autoreload=True: Automatically restarts server on file changes
- compiled_template_cache=False: Disables template caching
- static_hash_cache=False: Disables static file caching
- serve_traceback=True: Shows detailed error pages
For existing applications, you can modify settings after initialization:
application.settings.update({
"debug": True,
"autoreload": True,
"compiled_template_cache": False
})
Remember to disable debug mode in production! A common pattern is:
settings = {
"debug": os.environ.get("ENVIRONMENT") == "development",
"autoreload": os.environ.get("ENVIRONMENT") == "development"
}
If autoreload isn't working:
- Check file permissions - Tornado needs read access
- Ensure you're not using an IDE that locks files
- Verify you're modifying files in the correct directory
- Try setting
autoreload=True
separately fromdebug=True
When developing Tornado applications, manually editing settings like application.settings = {"Debug": True}
often fails to provide the expected automatic reloading behavior. This happens because debug mode requires multiple coordinated settings in Tornado.
The correct way to enable full debug features requires setting three key parameters in your Application constructor:
import tornado.web
import tornado.ioloop
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, Debug Mode!")
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
],
debug=True, # Enable debug mode
autoreload=True, # Auto-reload on file changes
compiled_template_cache=False # Disable template caching
)
if __name__ == "__main__":
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
Setting debug=True
in Tornado enables several developer-friendly features:
- Automatic reloading: The server restarts when Python files are modified
- Stack trace enhancement: Detailed error pages with tracebacks and source snippets
- Template no-cache: Templates are reloaded without server restart
For production environments, you should wrap the debug settings in environment detection:
def make_app():
is_production = os.getenv("ENVIRONMENT") == "production"
return tornado.web.Application([
(r"/", MainHandler),
],
debug=not is_production,
autoreload=not is_production,
compiled_template_cache=is_production
)
If automatic reloading doesn't work after proper configuration:
- Check file permissions - the process needs read/write access
- Verify you're modifying .py files, not just templates
- Ensure no syntax errors exist that prevent reloading