From the course: Complete Guide to Parallel and Concurrent Programming in Python

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Condition variable: Python demo

Condition variable: Python demo

- [Instructor] This Python program, to demonstrate using a condition variable, has a function named hungry person defined on line nine, which has an input parameter for a person ID number. The hungry person function will run as a thread that alternates with other hungry people threads to take servings of soup until it's all gone. The variable on line seven represents the number of soup servings left, and the slow cooker lid on line six is the lock to protect the soup serving's variable, so only one hungry person can change it at a time. Down within the while loop on line 11, the hungry person uses a context manager to lock the slow cooker lid. Then the if statement on line 13 compares their ID number with the number of soup servings remaining, modulo two, because this example creates two hungry person threads in its main section. If it's the current threads turn, and there's still soup left, then the hungry person will decrement the soup servings on line 14 and print a message that…

Contents