-
-
Save gotomypc/b09a60ea4493d494c5777fc71d3ae068 to your computer and use it in GitHub Desktop.
Example Tornado server with multiple processes running background tasks
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 | |
form tornado import ( | |
concurrent, | |
gen, | |
httpserver, | |
ioloop, | |
log, | |
process, | |
web | |
) | |
log.enable_pretty_logging() | |
# thread pool for async background tasks | |
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(f"Request accepted on process {process.task_id()}\n") | |
@gen.coroutine | |
def small_loop(): | |
while True: | |
yield print('in small loop!\n') | |
yield gen.sleep(10) | |
def make_app(): | |
return web.Application([ | |
(r"/", MainHandler) | |
], | |
debug=False) | |
def main(): | |
app = make_app() | |
# start server | |
server = httpserver.HTTPServer(app) | |
server.bind(8080) | |
server.start(0) # Processes = number of CPUs | |
# assign background task | |
if process.task_id() == 0: | |
ioloop.IOLoop.instance().spawn_callback(small_looop) | |
# assign periodic task | |
if process.task_id() == 1: | |
task = ioloop.PeriodicCallback( | |
lambda: print('period'), | |
2000) # 2000 ms | |
task.start() | |
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