tags:

views:

31

answers:

3

i want my thread to sleep when a queue is empty and just wake up when a data is put in it, should i use condition object? i have never used this object before! and i can,t find a simple example in python

A: 

Pass block=True to Queue.get().

Ignacio Vazquez-Abrams
could u explain plz
sia
In the thread that is supposed to sleep, call the `get(block=True)` method of your `Queue` object. Then that method will not return until it has successfully gotten an object out of the queue.
David Zaslavsky
A: 

There's a nice Linux Gazette article online that has code using a condition variable you might find useful: http://linuxgazette.net/107/pai.html. The article covers python threading in general. Section 4.4, almost at the bottom, covers condition objects.

Pierce
+1  A: 

If the queue object in question is bound to name q, just call q.get(): it will sleep patiently as long as the queue is empty, then return the queue's first item as soon as the queue is made non-empty by another thread executing a .put(whatever) on it. While the docs may not be stellarly clear about this, that's the default behavior of .get() when you call it without any argument, and indeed the most popular way for a thread to read from a queue (which is why it was made the default in the first place;-).

Alex Martelli