Skip to content

Instantly share code, notes, and snippets.

@puentesarrin
Created April 1, 2013 18:16
Show Gist options
  • Save puentesarrin/5286628 to your computer and use it in GitHub Desktop.
Save puentesarrin/5286628 to your computer and use it in GitHub Desktop.
Tornado, auto reloading/parsing options when config file is modified.
greeting = 'Hello, World'
import functools
import logging
import os
import tornado.ioloop
import tornado.web
from tornado.options import define, options
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write(options.greeting)
application = tornado.web.Application([
(r'/', MainHandler),
])
def load_options(config_file):
define('greeting', default='Hello, world', type=str,
help='Greeting string')
tornado.options.parse_config_file(config_file)
tornado.options.parse_command_line()
modify_time = None
def autoreload_config_file(config_file):
def reload_options_on_update(config_file):
modified = os.stat(config_file).st_mtime
global modify_time
if not modify_time:
modify_time = modified
return
if modify_time != modified:
logging.info('Modified config file, reloading options')
modify_time = modified
tornado.options.parse_config_file(config_file)
callback = functools.partial(reload_options_on_update, config_file)
tornado.ioloop.PeriodicCallback(callback, 500).start()
if __name__ == '__main__':
config_file = 'demo.conf'
load_options(config_file)
autoreload_config_file(config_file)
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment