diff --git a/CHANGELOG.md b/CHANGELOG.md index d912699e823d6143b4cbe6629b06167f1a178ac6..a32cf759098803268a09ae32596a3385dbb43edb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ -# v.25.a3 +# v.25.a7 * fixed circular import +* added __len__ to PeekableQueue # v2.25.5 diff --git a/satella/coding/concurrent/queue.py b/satella/coding/concurrent/queue.py index 8df1a49d96c99af5f524e00daee37a9b0cdf7109..fce6a064783cf0b38cee5287f0e5718f3806325a 100644 --- a/satella/coding/concurrent/queue.py +++ b/satella/coding/concurrent/queue.py @@ -13,13 +13,14 @@ class PeekableQueue(tp.Generic[T]): """ 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): super().__init__() self.queue = collections.deque() self.lock = threading.Lock() self.inserted_condition = Condition() + self.items_count = 0 def put(self, item: T) -> None: """ @@ -29,6 +30,7 @@ class PeekableQueue(tp.Generic[T]): """ with self.lock: self.queue.append(item) + self.items_count += 1 self.inserted_condition.notify() def put_many(self, items: tp.Sequence[T]) -> None: @@ -40,7 +42,7 @@ class PeekableQueue(tp.Generic[T]): with self.lock: items_count = 0 for item in items: - items_count += 1 + self.items_count += 1 self.queue.append(item) self.inserted_condition.notify(items_count) @@ -114,4 +116,7 @@ class PeekableQueue(tp.Generic[T]): guarantee that a subsequent get() will not block. :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