I want my thread to sleep when a queue is empty and wake up only when a data is put in it.
Should I use a condition object? I have never used this object before and I can’t find a simple example in python.
I want my thread to sleep when a queue is empty and wake up
Share
If the queue object in question is bound to name
q, just callq.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;-).