0

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.

Fonty
  • 239
  • 2
  • 11

1 Answers1

0

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.

Miguel Grinberg
  • 65,299
  • 14
  • 133
  • 152
  • Thanks. You're correct, in that I'm using gevent. The motor is connected to the server (i.e. a windows box) and I use a simple python function that moves the motor. I then have a while loop such that while the motor is moving, it does a mini sleep, then when it's no longer moving, it emits the 'move complete' bit. The funny thing is that when I was testing this all out purely with text to mimic the actual motor movement, it seemed to do it just fine with no problems or errors. But I will need to look into asynchronously running it, and also into the 'green thread' you mentioned. – Fonty May 28 '16 at 10:48