Created
October 23, 2017 21:11
-
-
Save marksilvis/b445eb83b7e103a3f4c7b58f4caac08d to your computer and use it in GitHub Desktop.
Tornado example with non-blocking background tasks executed in thread pool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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