Skip to content

Instantly share code, notes, and snippets.

@marksilvis
Created October 23, 2017 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save marksilvis/b445eb83b7e103a3f4c7b58f4caac08d to your computer and use it in GitHub Desktop.
Save marksilvis/b445eb83b7e103a3f4c7b58f4caac08d to your computer and use it in GitHub Desktop.
Tornado example with non-blocking background tasks executed in thread pool
import time
from tornado import (
concurrent,
gen,
httpserver,
ioloop,
log,
web
)
log.enable_pretty_logging()
# exector to schedule async task in background
executor = concurrent.futures.ThreadPoolExecutor(8)
class MainHandler(web.RequestHandler):
def get(self):
def task(t):
time.sleep(t)
print("\nCallback complete\n")
executor.submit(task, 10)
self.write("Request accepted\n")
def make_app():
return web.Application([
(r"/", MainHandler)
],
debug=False)
def main():
app = make_app()
# start server
server = httpserver.HTTPServer(app)
server.listen(8080)
print('starting IOLoop')
ioloop.IOLoop.current().start()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment