[Tutor] python queue

Kent Johnson kent37 at tds.net
Thu Sep 17 05:17:15 CEST 2009


On Wed, Sep 16, 2009 at 8:04 PM, Jeff Peery <jeffpeery at yahoo.com> wrote:
>
> Hello,
> Does anyone know if there is there a way to look at a queue's contents? Or prevent duplicate messages from being put into a queue? The docs don't show anything useful. The reason is that I'm collecting and drawing data. one thread collects, and one thread draws. each time one sample is collected by the collector thread, a "draw message" is put into the queue to notify the drawing thread. the dataset is shared between the threads and is continuously growing as data is appended. The queue is used to notify the drawing thread that it should draw. I use threading.Lock() to prevent any "sharing issues".
>
> The problem is that if the data collector thread is putting messages into the queue faster than the drawing thread is getting them, then the drawing thread is forced to redraw more times than it needs to and appears slow. However since the dataset is shared the drawing thread only needs to draw once to be updated for all the new samples. For example if 10 samples have been appended by the data collector thread while the drawing thread drew once, then there are now 10 messages for the drawing thread to get. yet it only needs to draw once to reflect the 10 samples. so there are 9 redraws that are a waste of energy.

 It sounds like what you really want is a flag. The collector thread
sets the flag when data is available, the draw thread clears the flag
when it starts to draw. If the flag is set multiple times before the
draw, it still only triggers one draw.

Take a look at threading.Event, it might work better than Queue.

You could try to use qsize() to avoid putting an item in the queue if
there is already something there, but you may have to introduce
additional locks to avoid race conditions.

Kent


More information about the Tutor mailing list