Thursday, 29 April 2021

Asyncio run Dash (Flask) server with another coroutine concurrently

I created a dash app to present information that another code is collecting, I want to run them both concurrently using the asyncio module in Python.

My code is using async functions and the Dash app (which is based on Flask) is blocking anything else from executing while serving.

I'm not sure if this is something that has to involve opening up more threads.

Here's my current code which only runs the main coroutine.

async def main():
    some code here...

    while True:
        try:
            await client.handle_message()
        except ConnectionClosedError as error:
            logger.error(error)
    
        for strategy in strategies:
            await asyncio.create_task(...)
            some code here...

async def run_dashboard():
    app = create_app()
    app.run_server('0.0.0.0', 5000, debug=False)


if __name__ == '__main__':
    some code here...

    # Currently just runs the main coroutine
    asyncio.run(main())

How can I run main and run_dashboard concurrently?



from Asyncio run Dash (Flask) server with another coroutine concurrently

No comments:

Post a Comment