From the course: Python Parallel and Concurrent Programming Part 1
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Rlock: Python demo - Python Tutorial
From the course: Python Parallel and Concurrent Programming Part 1
Rlock: Python demo
- [Instructor] To demonstrate using a reentrant lock in Python, we've modified the previous example we used to demonstrate a data race and mutual exclusion with two shoppers that are concurrently incrementing the amount of items to buy. In this version, we're counting the amount of garlic and potatoes to buy with the variables that are initialized on lines six and seven. There are two helper functions on lines 10 and 16 called add_garlic and add_potato, which increment the corresponding garlic_count or potato_count variables. And each of those functions acquire and release the same lock object, named pencil, to enforce mutual exclusion around those operations and prevent a data race. The shopper function simply uses a for loop on line 23 to execute those add_garlic and add_potato functions 10,000 times each. Down in the program's main section, we create and start two shopper threads, and then after they finish running,…
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.