From the course: Python Parallel and Concurrent Programming Part 2
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Condition variable: Python demo - Python Tutorial
From the course: Python Parallel and Concurrent Programming Part 2
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_person 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 slowcooker_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 slowcooker_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 thread's turn and there's still soup left, then hungry_person will decrement the soup_servings on line 14, and print a message that they took…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.