Search code examples
pythonmultithreadingthreadpool

Python Thread Pool on a Websocket Client


I have a web socket client that is receiving messages from a websocket server using asyncio.

async def web_socket(server):
async with websockets.connect(server) as websocket:
    message = await websocket.recv()
    t = Thread(target=message_helper.process_message,args(message))
    t.start()

The messages from the websocket server come in very quickly and it runs indefinitely. I want to setup a thread pool to ensure I have enough threads.

All the examples I have seen setup a queue (here is one example: Thread Pool with Queue. The examples that I have seen all have a predefined array of tasks then it starts processing them using the thread pool.

In my example above I don't see how I could implement this? As messages are coming in every second and I want to start a thread to process each message as it comes in. Is there a way to achieve what I am trying to do with a thread pool when I don't have a predefined array of tasks?


Solution

  • Would run_in_executor() do?

    async def web_socket(server):
        async with websockets.connect(server) as websocket:
            message = await websocket.recv()
            loop.run_in_executor(None, message_helper.process_message, args(message))