Queue peek?

Floris Bruynooghe floris.bruynooghe at gmail.com
Wed Mar 3 17:17:32 CET 2010


On Mar 2, 6:18 pm, Raymond Hettinger <pyt... at rcn.com> wrote:
> On Mar 2, 8:29 am, Veloz <michaelve... at gmail.com> wrote:
>
> > Hi all
> > I'm looking for a queue that I can use with multiprocessing, which has
> > a peek method.
>
> > I've seen some discussion about queue.peek but don't see anything in
> > the docs about it.
>
> > Does python have a queue class with peek semantics?
>
> Am curious about your use case?  Why peek at something
> that could be gone by the time you want to use it.
>
>   val = q.peek()
>   if something_i_want(val):
>        v2 = q.get()         # this could be different than val
>
> Wouldn't it be better to just get() the value and return if you don't
> need it?
>
>   val = q.peek()
>   if not something_i_want(val):
>       q.put(val)

What I have found myself wanting when thinking of this pattern is a
"q.put_at_front_of_queue(val)" method.  I've never actually used this
because of not having such a method.  Not that it's that much of an
issue as I've never been completely stuck and usually found a way to
solve whatever I was trying to do without peeking, which could be
argued as a better design in the first place.  I was just wondering if
other people ever missed the "q.put_at_front_of_queue()" method or if
it is just me.

Regards
Floris

PS: assuming "val = q.get()" on the first line



More information about the Python-list mailing list