[issue35118] Add peek() or first() method in queue
New submission from Windson Yang
Change by Raymond Hettinger
Raymond Hettinger
Windson Yang
Raymond Hettinger
Change by Windson Yang
Raymond Hettinger
Change by Raymond Hettinger
Alexey Volkov
For Queue, I'm not sure I've ever seen any use case for peek. What do you have in mind?
I want to examine the first (oldest) element in queue and remove it if it's too old.
The queue should not be modified unless the oldest element is too old.
----------
nosy: +Ark-kun
_______________________________________
Python tracker
Raymond Hettinger
I want to examine the first (oldest) element in queue and remove it if it's too old.
Why not just dismiss older queue entries during a normal get() operation? Or just use a plain deque with access guarded by a lock.
FWIW, the standard library queue module doesn't have a straight-forward way to implement a peek() method. The module guarantees that the underlying data structure is only accessed with _init, _qsize, _get, and _put.
That would be difficult to do atomically with a Queue object.
----------
_______________________________________
Python tracker
Alexey Volkov
Why not just dismiss older queue entries during a normal get() operation? Or just use a plain deque with access guarded by a lock.
You do not know whether the item needs to be removed until you see it. And to see it you need to get it. And if you get it, you cannot push it back to the start of the queue.
FWIW, the standard library queue module doesn't have a straight-forward way to implement a peek() method.
I currently use `q.deque[0]`
Or just use a plain deque with access guarded by a lock.
I can. But technically the same applies to almost any queue usage.
----------
_______________________________________
Python tracker
Raymond Hettinger
FWIW, the standard library queue module doesn't have a straight-forward way to implement a peek() method.
I currently use `q.deque[0]`
The Queue class is only allowed to call _init, _qsize, _put, and _get. It is not allowed to directly touch the underlying data structure. Otherwise, subclasses relying on the abstraction would fail.
----------
_______________________________________
Python tracker
participants (3)
-
Alexey Volkov
-
Raymond Hettinger
-
Windson Yang