An improvement to Queue

Alex Martelli aleax at aleax.it
Thu Jan 31 11:20:48 EST 2002


"Michael Abbott" <michael at rcp.co.uk> wrote in message
news:Xns91A7A14C232EBmichaelrcpcouk at 194.238.50.13...
    ...
> >> 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
    ...
> An interesting event might be a socket read ready event: I read what I can
> from the socket, and queue the results for processing on the worker
thread.
> I don't particularly want to queue a separate do_the_work event for each
> incoming block, particularly as the worker thread might be really quite
> busy at times (in fact, at the moment I'm running 100% cpu for minutes at
a
> stretch; Python has its challenges).

Important scenario, but why would you want to queue a do-the-work when
a different queue WAS empty?  The natural point in time at which to
queue would seem to be "when enough stuff has come from the socket"
(presumably determined by examining the stuff).

> Of course without my hack I can still work around by testing whether
> my_queue.qsize()==1, assuming only one thread is generating work...

Yes, although that's always a bit fragile (what you just posted to
my_queue can already have been consumed when you get around to
testing size -- so maybe <= 1 is safer even for one producer, as
long as that one producer isn't also the sole consumer in which
case Queue may be inappropriate anyway).


Alex






More information about the Python-list mailing list