An improvement to Queue

Alex Martelli aleax at aleax.it
Thu Jan 31 09:53:15 EST 2002


"Michael Abbott" <michael at rcp.co.uk> wrote in message
news:Xns91A793581DD79michaelrcpcouk at 194.238.50.13...
> I would like to suggest a simple and fully backwards compatible
improvement
> to the standard Queue class.  How do I go about arranging that this
> actually happens?
>
> The proposal is very simple:
>
> 1.  The Queue.get() method be modified to return a flag indicating whether

I think you mean .put, both from logic and from the example below.

> the queue has just changed state from empty to non-empty.  This allows the
> generating process to know whether a "wake-up" signal needs to be passed
to
> the client thread, and is useful for non-blocking use of the queue.

In this case, for symmetry, you might also want .get to also return a
boolean about whether the Queue transitioned from full to non-full.

I'm not clear on why the 'client' (getting?) thread would be waiting
on a wakeup signal rather than on the queue itself.  Example please?


> 2.  A new Queue.get_all() method be added which returns the entire content
> of the queue in one gulp.  Of course this can be implemented by looping on
> Queue.get(), but get_all can be *much* more efficient.

Yes, I can see the efficiency advantage here, when there's just one
consumer (thread calling get) on the queue object, e.g. the example
I posted earlier today could be tweaked to make use of this and gain
some epsilon in terms of efficiency (less locking overhead): the
main thread needs N results, it could gulp whatever number is
available at each given time rather than doing get N times.


Alex






More information about the Python-list mailing list