Wait on stop event when possible

Generally eliminate the `while True` loops while waiting for a stop
event and prefer to condition the loops on if the stop event is set,
blocking on that where it makes sense.  This generally comes in 3
flavors.  First and simplest, when there is a sleep and the stop event
is the only thing the loop blocks on, instead do a check using
`stop_event.wait(timeout)` to instead block on the stop event for the
designated amount of time. Second, when there is a different event that
is blocking in the loop, condition the loop on `stop_event.is_set()`
rather than breaking when it is set. Finally, when there is a separate
internal condition that requires a counter, have the loop iterate over
the counter and use `if stop_event.wait(timeout)` internal to the loop.
This commit is contained in:
Sean Vig
2021-05-21 11:39:14 -04:00
committed by Blake Blackshear
parent f4bc68d396
commit 57864f2be6
7 changed files with 27 additions and 63 deletions

View File

@@ -212,15 +212,7 @@ class CameraWatchdog(threading.Thread):
)
time.sleep(10)
while True:
if self.stop_event.is_set():
stop_ffmpeg(self.ffmpeg_detect_process, self.logger)
for p in self.ffmpeg_other_processes:
stop_ffmpeg(p["process"], self.logger)
p["logpipe"].close()
self.logpipe.close()
break
while not self.stop_event.wait(10):
now = datetime.datetime.now().timestamp()
if not self.capture_thread.is_alive():
@@ -248,8 +240,11 @@ class CameraWatchdog(threading.Thread):
p["cmd"], self.logger, p["logpipe"], ffmpeg_process=p["process"]
)
# wait a bit before checking again
time.sleep(10)
stop_ffmpeg(self.ffmpeg_detect_process, self.logger)
for p in self.ffmpeg_other_processes:
stop_ffmpeg(p["process"], self.logger)
p["logpipe"].close()
self.logpipe.close()
def start_ffmpeg_detect(self):
ffmpeg_cmd = [
@@ -451,10 +446,7 @@ def process_frames(
fps_tracker = EventsPerSecond()
fps_tracker.start()
while True:
if stop_event.is_set():
break
while not stop_event.is_set():
if exit_on_empty and frame_queue.empty():
logger.info(f"Exiting track_objects...")
break