operating system: windows 10
python 3.7.6
- I have a task function
def mytest(task_queue):
while True:
print(task_queue.get())
- I want run sub thread and waiting for others put something into
task_queue. - If I use
concurrent.futures.ThreadPoolExecutor().submit()start thread, then put something into queue, it will block,task_queue.put(1)never run.
if __name__ == '__main__':
import queue
task_queue = queue.Queue()
task_queue.put(0)
with concurrent.futures.ThreadPoolExecutor() as executor:
executor.submit(mytest, task_queue)
task_queue.put(1)
task_queue.put(2)
# only print 0, then block
- If I start thread by
Thread().start(), it works as I expect.
if __name__ == '__main__':
import queue
task_queue = queue.Queue()
task_queue.put(0)
t1 = threading.Thread(target=mytest, args=(task_queue,))
t1.start()
task_queue.put(1)
task_queue.put(2)
# print 0, 1, 2. but the main thread does not exit
- But I don't think either of these methods will block the code because they just start the thread.
- So I have 2 question:
- Why does
submit()block the code? - Why main thread does not exit when use
start()to start sub thread withoutjoin()?
- Why does
THX