From the course: Complete Guide to Parallel and Concurrent Programming in Python
Unlock the full course today
Join today to access over 24,900 courses taught by industry experts.
Starvation: Python demo - Python Tutorial
From the course: Complete Guide to Parallel and Concurrent Programming in Python
Starvation: Python demo
- [Instructor] To demonstrate thread starvation in Python, we'll modify the dining philosopher's example by adding a local variable within the philosopher function to keep track of how many pieces of sushi each philosopher thread gets to eat. I'm going to name that sushi_eaten, and I'll initialize it to zero. We'll increment the sushi eaten variable every time the philosopher takes a piece of sushi. Sushi_eaten plus equals one. And at the end of the philosopher function, after the while loop has finished, we'll print out the number of pieces that it took. Print(name, 'took' sushi_eaten, 'pieces'). There are currently 5,000 pieces of sushi up for grabs on line nine, so I'll switch over to a command prompt and run this program. When it finishes, we see that each of the three philosophers got a different amount of sushi, and it's not particularly fair. Olivia took way more sushi than Barron or Steve. She has over 3,000 of the pieces. But it's not because she's greedy. In this case, it's…
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.