You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the Chapter 12: communicating between threads "Solution" section (bottom of p493) there is an example in which the Queue.task_done method is used along with Queue.join to wait for all items produced by the "producer" function to be consumed.
However (and unless I am misunderstanding), because the queue has no items in it before the threads start, unless the producer function immediately puts an item (before the q.join() executes in the main thread) into the queue, the queue will immediately join. For example, if you have something like:
from queue import Queue
from threading import Thread
import time
def producer(out_q):
time.sleep(5)
out_q.put('hello')
def consumer(in_q):
h = in_q.get()
print(h + ' world!')
in_q.task_done()
q = Queue()
t1 = Thread(target=producer, args=(q,))
t2 = Thread(target=consumer, args=(q,))
t1.start()
t2.start()
q.join()
print('q joined!')
You will see:
q joined!
hello world!
I guess the fix would be putting in an initial sentinel before starting the threads?
Love the book by the way!
The text was updated successfully, but these errors were encountered:
This is a good observation. Another possible fix is to join with the producer thread before performing the task join operation. I'll make a note in text for errata.
In the Chapter 12: communicating between threads "Solution" section (bottom of p493) there is an example in which the Queue.task_done method is used along with Queue.join to wait for all items produced by the "producer" function to be consumed.
However (and unless I am misunderstanding), because the queue has no items in it before the threads start, unless the producer function immediately puts an item (before the q.join() executes in the main thread) into the queue, the queue will immediately join. For example, if you have something like:
from queue import Queue
from threading import Thread
import time
Hi!
In the Chapter 12: communicating between threads "Solution" section (bottom of p493) there is an example in which the Queue.task_done method is used along with Queue.join to wait for all items produced by the "producer" function to be consumed.
However (and unless I am misunderstanding), because the queue has no items in it before the threads start, unless the producer function immediately puts an item (before the q.join() executes in the main thread) into the queue, the queue will immediately join. For example, if you have something like:
You will see:
I guess the fix would be putting in an initial sentinel before starting the threads?
Love the book by the way!
The text was updated successfully, but these errors were encountered: