mirror of
https://github.com/blakeblackshear/frigate.git
synced 2025-07-30 13:48:07 +02:00
Add thread-safety to LimitedQueue by implementing a lock for put and get methods (#7053)
This commit is contained in:
parent
f48dd8c1ab
commit
30dfdf47d4
@ -78,8 +78,10 @@ class LimitedQueue(FFQueue):
|
|||||||
self.size = multiprocessing.RawValue(
|
self.size = multiprocessing.RawValue(
|
||||||
ctypes.c_int, 0
|
ctypes.c_int, 0
|
||||||
) # Add a counter for the number of items in the queue
|
) # Add a counter for the number of items in the queue
|
||||||
|
self.lock = multiprocessing.Lock() # Add a lock for thread-safety
|
||||||
|
|
||||||
def put(self, x, block=True, timeout=DEFAULT_TIMEOUT):
|
def put(self, x, block=True, timeout=DEFAULT_TIMEOUT):
|
||||||
|
with self.lock: # Ensure thread-safety
|
||||||
if self.maxsize > 0 and self.size.value >= self.maxsize:
|
if self.maxsize > 0 and self.size.value >= self.maxsize:
|
||||||
if block:
|
if block:
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
@ -94,13 +96,15 @@ class LimitedQueue(FFQueue):
|
|||||||
return super().put(x, block=block, timeout=timeout)
|
return super().put(x, block=block, timeout=timeout)
|
||||||
|
|
||||||
def get(self, block=True, timeout=DEFAULT_TIMEOUT):
|
def get(self, block=True, timeout=DEFAULT_TIMEOUT):
|
||||||
|
item = super().get(block=block, timeout=timeout)
|
||||||
|
with self.lock: # Ensure thread-safety
|
||||||
if self.size.value <= 0 and not block:
|
if self.size.value <= 0 and not block:
|
||||||
raise Empty
|
raise Empty
|
||||||
self.size.value -= 1
|
self.size.value -= 1
|
||||||
return super().get(block=block, timeout=timeout)
|
return item
|
||||||
|
|
||||||
def qsize(self):
|
def qsize(self):
|
||||||
return self.size
|
return self.size.value
|
||||||
|
|
||||||
def empty(self):
|
def empty(self):
|
||||||
return self.qsize() == 0
|
return self.qsize() == 0
|
||||||
|
Loading…
Reference in New Issue
Block a user