I have a conventional multithreaded producer/consumer pattern with a queue.Queue in between. This implementation of queues is thread safe. The consumer can consume the data in two ways:
Blocking
while self.running:
data = self.receiver_q.get(block=True)
# do something with data
Non-blocking
while self.running:
try:
data = self.receiver_q.get(block=False)
except queue.Empty:
continue
# do something with data
In the blocking method, the consumer waits until there is data in the queue. During this time, is the consumer holding the lock on the queue? I can't imagine a way for it to hold the lock while allowing the queue to have new data placed on it.
Also, is there a performance difference between the two patterns?