Search code examples
python-3.xpython-asynciotornado

Multiprocessing in tornado


I want to have two non-blocking processes running synchronously, the one is a while True loop which caches data every minute, and the second one is a code such below:

def create_and_start_app():
    app = Application([
        ('/dashboard', Dashboard)
    ],
    debug=RuntimeConfig.DEBUG_MODE)
    http_server = HTTPServer(app)
    http_server.listen(RuntimeConfig.APP_PORT)
    print(f'Listening on http:/{RuntimeConfig.APP_HOST}:{RuntimeConfig.APP_PORT}')
    IOLoop.current().start()

I tried IOLoop.current.spawn_call_back approach and also concurrent.futures.ThreadPoolExecutor but it seems I didn't get the point of documentation because every approach I test, blocks with while True in caching method and program doesn't execute next process. Thank you if you show me a sample code which fits into this case.


Solution

  • time.sleep is a blocking function. It suspends the whole thread, therefore it's not ideal to use in an async program. You ought to use an asynchronous equivalent of it (such as tornado.gen.sleep or asyncio.sleep which will only suspend the coroutine, not the whole thread.

    Example:

    from tornado import gen
    
    async def my_coroutine():
        while True:
            # do somehting ...
            await gen.sleep(5 * 60) # pause while loop for 5 minutes