Skip to content

Instantly share code, notes, and snippets.

@hclivess
Created June 5, 2019 23:10
Show Gist options
  • Save hclivess/e56c5717a78eef2f19c514a70ae60ae4 to your computer and use it in GitHub Desktop.
Save hclivess/e56c5717a78eef2f19c514a70ae60ae4 to your computer and use it in GitHub Desktop.
Tornado Web Background Thread (using separately updated object)
import json
import tornado.ioloop
import tornado.web
import dator
import threading
import time
class MainHandler(tornado.web.RequestHandler):
def initialize(self, updater):
self.updater = updater
#self.updater.update()
def get(self):
#call fetcher
self.updater.update()
#render
self.render("chart.html",
stata = self.updater.history.stata,
)
def make_app():
return tornado.web.Application([
(r"/", MainHandler, {'updater': updater}),
(r"/static/(.*)", tornado.web.StaticFileHandler, {"path": "static"}),
])
class ThreadedClient(threading.Thread):
def __init__(self, updater):
threading.Thread.__init__(self)
def run(self):
while True:
updater.update()
time.sleep(10)
if __name__ == "__main__":
updater = dator.Updater()
client_instance = ThreadedClient(updater)
client_instance.start()
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment