-
-
Save gotomypc/41d057b08e46001196893a0693de4d23 to your computer and use it in GitHub Desktop.
Tornado example with non-blocking background tasks executed in thread pool
This file contains hidden or 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