Skip to content
Snippets Groups Projects
Unverified Commit 332b6e40 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

added __len__ to PeekableQueue

parent 14f32728
No related branches found
No related tags found
No related merge requests found
Pipeline #62414 failed with stages
in 1 minute and 21 seconds
# v.25.a3 # v.25.a7
* fixed circular import * fixed circular import
* added __len__ to PeekableQueue
# v2.25.5 # v2.25.5
......
...@@ -13,13 +13,14 @@ class PeekableQueue(tp.Generic[T]): ...@@ -13,13 +13,14 @@ class PeekableQueue(tp.Generic[T]):
""" """
A thread-safe FIFO queue that supports peek()ing for elements. A thread-safe FIFO queue that supports peek()ing for elements.
""" """
__slots__ = ('queue', 'lock', 'inserted_condition') __slots__ = ('queue', 'lock', 'inserted_condition', 'items_count')
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.queue = collections.deque() self.queue = collections.deque()
self.lock = threading.Lock() self.lock = threading.Lock()
self.inserted_condition = Condition() self.inserted_condition = Condition()
self.items_count = 0
def put(self, item: T) -> None: def put(self, item: T) -> None:
""" """
...@@ -29,6 +30,7 @@ class PeekableQueue(tp.Generic[T]): ...@@ -29,6 +30,7 @@ class PeekableQueue(tp.Generic[T]):
""" """
with self.lock: with self.lock:
self.queue.append(item) self.queue.append(item)
self.items_count += 1
self.inserted_condition.notify() self.inserted_condition.notify()
def put_many(self, items: tp.Sequence[T]) -> None: def put_many(self, items: tp.Sequence[T]) -> None:
...@@ -40,7 +42,7 @@ class PeekableQueue(tp.Generic[T]): ...@@ -40,7 +42,7 @@ class PeekableQueue(tp.Generic[T]):
with self.lock: with self.lock:
items_count = 0 items_count = 0
for item in items: for item in items:
items_count += 1 self.items_count += 1
self.queue.append(item) self.queue.append(item)
self.inserted_condition.notify(items_count) self.inserted_condition.notify(items_count)
...@@ -114,4 +116,7 @@ class PeekableQueue(tp.Generic[T]): ...@@ -114,4 +116,7 @@ class PeekableQueue(tp.Generic[T]):
guarantee that a subsequent get() will not block. guarantee that a subsequent get() will not block.
:return: approximate size of the queue :return: approximate size of the queue
""" """
return len(self.queue) return self.items_count
def __len__(self):
return self.items_count
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment