[Tutor] Changing instance attributes in different threads

Kent Johnson kent37 at tds.net
Thu Feb 9 00:14:14 CET 2006


Michael Lange wrote:
> Sorry, I missed to insert the time.sleep(0.1) I used in my original while loop into the example above.
> The reason for using time.sleep() is that I need to avoid lots of loops over an empty buffer.
> The amount of time until the producer thread reads a new data fragment into the buffer may
> be significant, depending on the fragment size requested by the driver (e.g my fm801 card
> wants fragments of 16384 bytes which is about 0.09 audio seconds). On the other hand the
> buffer may contain hundreds of kB of data if other processes cause a lot of disk I/O.

Using Queue.get() will do this for you automatically. If there is no 
data it will block until something is added to the queue and you avoid a 
  polling loop. If there is data it will return it quickly.
> 
>>You should be able to take the try/except out of your version, since 
>>this code is the only consumer from the Queue, if queue.empty() is 
>>false, queue.get() should succeed.
>>
> Not if I call it with block=0; if I understand the docs correctly the queue will raise a Queue.Empty exception
> if the queue is currently locked by another thread. 

No, the block flag controls whether the call will wait until something 
is in the queue or return immediately. The call to Queue.get() will 
always block waiting for the lock that controls access to the Queue; it 
can't even tell if the Queue is empty until it gets this lock.

Kent



More information about the Tutor mailing list