Skip to content

Instantly share code, notes, and snippets.

@hclivess
Last active June 19, 2022 23:32
Show Gist options
  • Select an option

  • Save hclivess/6e43a4e3e50824cbb8662feb045ef034 to your computer and use it in GitHub Desktop.

Select an option

Save hclivess/6e43a4e3e50824cbb8662feb045ef034 to your computer and use it in GitHub Desktop.
Simplest possible threaded Tornado server
import tornado.ioloop
import tornado.web
import threading
import time
class ReadHandler(tornado.web.RequestHandler):
def get(self):
self.write("hello world")
def make_app():
return tornado.web.Application([
(r"/", ReadHandler),
(r"/static/(.*)", tornado.web.StaticFileHandler, {"path": "static"}),
])
class ThreadedClient(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
while True:
try:
print("Background process running")
run_interval = 360
print(f"Sleeping for {run_interval / 60} minutes")
time.sleep(run_interval)
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
background = ThreadedClient()
background.start()
print("Background process started")
app = make_app()
app.listen(1236)
print("Main process starting")
tornado.ioloop.IOLoop.current().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment