I’ve got two threads in my application. One that puts values in a Queue, and another that pulls them from the Queue and processes them.
I am faced with a dilemma when shutting the application down. The thread that processes items in the Queue is stuck on:
item = request_queue.get() # this call blocks until an item is available
The only thing that will terminate the thread is if another item is added to the Queue – and since the main thread doesn’t add anything (because it’s shutting down), the application locks.
So… how can I instruct Queue.get() to somehow return even if there is nothing on the Queue?
The answer it turns out is quite simple. Pick a value that would be invalid for the code that processes the
Queue(Noneis ideal for that) and push that into theQueue. Then have theQueueprocessing thread quit when it gets the value: