Skip to content
Advertisement

Python: will a thread ever unblock while hanging on a `Queue.get(block=True)` call if the queue is suddenly destroyed from another thread/process?

TLDR: Would a blocking get be unblocked by a queue that would be terminated in some way?


Long question:

Okay, so I know that the thread will hang if the queue (multiprocessing.Queue) remains empty forever while trying to fetch something from it with a blocking get.

But suppose now that in another thread or process, I close the queue with queue.close(), call queue.cancel_join_thread(), and then also queue.join_thread().

With the blocking “get” raise an exception or something upon trying to kill the queue like that?

Thanks!

Advertisement

Answer

Yes, an exception is raised if the queue is closed.

From the latest documentation:

Under Queue.get() the following statement occurs:

“Changed in version 3.8: If the queue is closed, ValueError is raised instead of OSError.”

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement