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.

Livelock: Python demo

Livelock: Python demo

- [Instructor] To demonstrate a livelock in Python, we'll modify the original dining philosopher's example that we use to demonstrate a deadlock. Now, we have not implemented the priority strategy for lock ordering to prevent a deadlock. So if I run this program with python livelock.py, sure enough, after several iterations, it hits a deadlock. The philosophers have acquired their first chopstick lock, and they're stuck waiting for their second one to become available. One possible way to fix this problem would be to have the philosophers release the lock on their first chopstick if the second chopstick is not available when they try to take it. That will give another philosopher a chance to take that first chopstick and hopefully prevent a deadlock. To implement that, I'll modify the acquire method for the second chopstick on line 15 to be non-blocking by setting the blocking value to false. I'll also add an if statement to execute if that lock was not taken. And inside of that if…

Contents