Live update image while using socketio
Question: I'm using python to push every nth image from a scientific camera to a web
page. The webpage updates the image using `.replace()`. It's probably
important to note that this is not a webcam - it's a scientific cam. I'm using
the developer software to save an image to the server every n seconds, which
is then grabbed by the client.

I'm using HTML, JavaScript, Jquery, and python.

I also have a bunch of web sockets (using socketio on the client side, and
flask on the server side) such that when a button is clicked it emits a
command to move a motor connected to the server. When this happens, the image
stops updating until that emit function on the server is finished.

I have thought about using a background thread to push the image to the server
every n seconds (based on user request for refresh rate), but not sure this
would help at all... or where to start, really.

Thanks in advance.


Answer: I assume you are running an eventlet or gevent web server, since you mention
you are using WebSocket.

Whenever you have a potentially long task, be it a HTTP request or a Socket.IO
event handler, you have to make sure it is done asynchronously, so that the
web server isn't blocked and can continue to respond.

You don't explain how your motor movement is handled, but it sounds like you
might need to put that in a background thread (green thread, not regular
Python thread), and make frequent calls to `sleep()` so that other coroutines
get a chance to run while the bg thread is running. I think this will enable
the images to continue flowing.

