forked from Github/frigate
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:
committed by
Blake Blackshear
parent
f4bc68d396
commit
57864f2be6
@@ -221,11 +221,7 @@ class EventProcessor(threading.Thread):
|
||||
return True
|
||||
|
||||
def run(self):
|
||||
while True:
|
||||
if self.stop_event.is_set():
|
||||
logger.info(f"Exiting event processor...")
|
||||
break
|
||||
|
||||
while not self.stop_event.is_set():
|
||||
try:
|
||||
event_type, camera, event_data = self.event_queue.get(timeout=10)
|
||||
except queue.Empty:
|
||||
@@ -272,6 +268,8 @@ class EventProcessor(threading.Thread):
|
||||
del self.events_in_process[event_data["id"]]
|
||||
self.event_processed_queue.put((event_data["id"], camera))
|
||||
|
||||
logger.info(f"Exiting event processor...")
|
||||
|
||||
|
||||
class EventCleanup(threading.Thread):
|
||||
def __init__(self, config: FrigateConfig, stop_event):
|
||||
@@ -398,19 +396,8 @@ class EventCleanup(threading.Thread):
|
||||
)
|
||||
|
||||
def run(self):
|
||||
counter = 0
|
||||
while True:
|
||||
if self.stop_event.is_set():
|
||||
logger.info(f"Exiting event cleanup...")
|
||||
break
|
||||
|
||||
# only expire events every 5 minutes, but check for stop events every 10 seconds
|
||||
time.sleep(10)
|
||||
counter = counter + 1
|
||||
if counter < 30:
|
||||
continue
|
||||
counter = 0
|
||||
|
||||
# only expire events every 5 minutes
|
||||
while not self.stop_event.wait(300):
|
||||
self.expire("clips")
|
||||
self.expire("snapshots")
|
||||
self.purge_duplicates()
|
||||
@@ -420,3 +407,5 @@ class EventCleanup(threading.Thread):
|
||||
Event.has_clip == False, Event.has_snapshot == False
|
||||
)
|
||||
delete_query.execute()
|
||||
|
||||
logger.info(f"Exiting event cleanup...")
|
||||
|
||||
Reference in New Issue
Block a user