Thread communication using a queue
As discussed earlier, threading can be complicated when threads need to share data or resources. As we saw, the Python threading module provides many synchronization primitives, including semaphores, condition variables, events, and locks. While these options exist, it is considered a best practice to instead concentrate on using the module queue. Queues are much easier to deal with and make threaded programming considerably safer, as they effectively funnel all access to a resource of a single thread and allow a cleaner and more readable design pattern.
We will simply consider these four queue methods:
put()
: This puts an item in the queueget()
: This removes and returns an item from the queuetask_done()
: This needs to be called each time an item has been processedjoin()
: This blocks until all items have been processed
How to do it…
In this example, we will see how to use the threading module with the queue module. Also, we have two entities here that try...