Reputation: 559
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?
Upvotes: 1
Views: 1097
Reputation: 77827
(1) No, the consumer is not holding a lock; it's merely blocked until the request is satisfied, but the queue is still available. If there are multiple consumers, then this one is in the pool of potential recipients; the resolution function will choose the order of satisfaction.
(2) Any performance difference depends on the implementation. Don't ask us -- you have the foremost authority in front of you: your computer. Use a test scenario of your choice, and the timeit
facility.
Upvotes: 2