Condition variables

Let's imagine that somehow, we had a way through which we could tell our Thread-1 to wait until Thread-2 has made some data available for consumption. This is exactly what condition variables allow us to do. They allow us to synchronize two threads that depend on a shared resource. To understand more about this, let's take a look at the following code sample, which creates two threads, one that feeds in the email ID and another thread that is responsible for sending the emails:

# condition_variable.pyimport threadingclass EmailQueue(threading.Thread):    def __init__(self, email_queue, max_items, condition_var):        threading.Thread.__init__(self)        self.email_queue = email_queue        self.max_items = max_items self.condition_var = ...

Get Hands-On Enterprise Application Development with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.