Skip to content Skip to sidebar Skip to footer

Making A Process Non-blocking Inside A Websocket Asyncio

I am using websocket library in python to communicate with a JS code in front end. Here is a summary of what I am doing. When I receive a message from the client, I call __process_

Solution 1:

If __process_msg is invoking CPU-bound or otherwise non-async (blocking) code, you should off-load it to a thread pool to avoid halting the event loop while the code is running. The easiest way to do that is to use the run_in_executor event loop method:

async def __process_msg(self, message, websocket):
    #value = long_running_function(args...)
    loop = asyncio.get_event_loop()
    value = await loop.run_in_executor(None, long_running_function, args...)

Post a Comment for "Making A Process Non-blocking Inside A Websocket Asyncio"