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
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.
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;-).