0

I want to pull image from bunch of cameras two times per second. I see a lot of usage of "while True" loops for that, like this:

while True:
    image = getimage()
    action(image)
    time.sleep(0.5)

But it doesn't feel correct for a system that should work 24/7.
Is it correct way to do such thing or it's better to use some sort of scheduler? In that case can you give an example?

Tony
  • 1
  • 1
  • Your question is rather opinion based and not very clear. What's your problem? That you need it to be exactly 2 FPS, i.e. your sleep depends on the processing time of the previous iteration? Or is it the use of "while True" that bothers you from a best-practice point of view? – Jeppe Jun 28 '22 at 11:15
  • Yep, just from a best-practice point of view. – Tony Jun 28 '22 at 11:18
  • Then this is perhaps more suited for [https://codereview.stackexchange.com/](https://codereview.stackexchange.com/). But I don't think there's anything wrong with the while loops for a continuously running service. The condition might ideally be something that can be terminated, e.g. `while self.running` or using a break in the loop. Error handling within the loop is of course important, since any error may or will terminate the loop. A scheduler approach might not have that problem. – Jeppe Jun 28 '22 at 11:25

0 Answers0