Reading a Multiprocessing Pipe on a WebSocket connection using Flask

I have a development API being served by Flask that spawns a bunch of synchronous processes using Multiprocess. These processes are listeners that keep running indefinitely and react when something interesting happens. They also watch for Pipe messages just in case the API needs them to do something.

I also have a front-end that displays real-time information about the API using WebSockets. Now, I would like to have my front-end to display real-time information about the listeners and I’m a little confused about how to implement it.

I’m thinking on keep reading from the API end Pipe using pipe.recv() whenever a WebSocket connection with the front-end is requested. And if the listeners find something interesting they would send this information over the Pipe to my API, and then the API would emit a WebSocket event to send this information to the front-end.

My main concern is about performance issues on Flask. I know that in production Flask would be wrapped by a server that could handle multiple requests/connections at the same time. But would an open WebSocket connection always reading from a pipe be too much?

Sorry for not posting some sample code to illustrate my idea. I’ll do it as soon as I can.

Source: Python Questions

LEAVE A COMMENT