2

I have a flask server with socketIO (flask-socketIO) that produces many screen grabs per second. Each time it grabs the screen it sends the screen (as binary data) to the flask-socketIO server for processing. This processing is computationally intense such that when I increase the FPS for screen grabs what seems to happen is the websocket connection stalls and (I think) the message queue is getting filled. This image process will run faster if its on its own thread/process. I have tested this by running a loop on the image process function and having it read the images.

Currently we call it like this:

@socketio.on('img')
def handle_msg(msg):
    file_bytes = np.asarray(bytearray(msg), dtype=np.uint8)
    img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
    res = PE.predict_img(img)

Where img is the image sent over the websocket.

On client I have something like:

 const send = () => {
     canvas.getContext('2d').drawImage(v, 0, 0, canvas.width, canvas.height);
     canvas.toBlob(blob => socket.emit('img', blob));
     console.log('loop');
 }
 const sendloop = setInterval(send ,1000/useFrameRate);

If I wanted to run this function in another thread do I have to loop over it and have the process read the image or can I do it in a way where I send the image (the same way I am sending it now) by calling the function when I need to. I am trying to avoid looping since there is already a loop capturing the frames, I don't believe it makes sense to set up another loop to process the image on the backend, but maybe I am wrong.

EDIT: Maybe something similar to this?: How to run Python custom objects in separate processes, all working on a shared events queue?

I was thinking it may be possible to have a function that loops forever on a separate thread and reads a queue?

Kevin
  • 3,077
  • 6
  • 31
  • 77

0 Answers0