Skip to content

Instantly share code, notes, and snippets.

@giupo
Created October 6, 2016 17:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giupo/f720f45748d5016eb440c1541b0535d1 to your computer and use it in GitHub Desktop.
Save giupo/f720f45748d5016eb440c1541b0535d1 to your computer and use it in GitHub Desktop.
import tornado.web
import tornado.httpserver
import os
class PageOneHandler(tornado.web.RequestHandler):
def get(self):
self.write(str(self.application.configs) + "\n")
def post(self):
p = int(self.get_argument("p"))
self.application.configs["some_data"] = p
self.write(str(self.application.configs) + "\n")
class PageTwoHandler(tornado.web.RequestHandler):
def get(self):
self.write(str(self.application.configs) + "\n")
def post(self):
p = int(self.get_argument("p"))
self.application.configs["some_data"] = p
self.write(str(self.application.configs) + "\n")
class MyApplication(tornado.web.Application):
def __init__(self, configs):
handlers = [
('/pageone', PageOneHandler),
('/pagetwo', PageTwoHandler)
]
settings = dict(
template_path='/templates',
static_path='/static',
debug=False)
self.configs = configs
tornado.web.Application.__init__(self, handlers, **settings)
# Run the instance
application = MyApplication({
'some_data': 1, # etc.
})
http_server = tornado.httpserver.HTTPServer(application)
def update_configs():
print(os.getpid(), " updating", str(application.configs))
http_server.bind(8888)
http_server.start(2)
# Callback function to update configs
some_time_period = 1000 # Once an second
tornado.ioloop.PeriodicCallback(update_configs, some_time_period).start()
tornado.ioloop.IOLoop.instance().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment