From the course: Python Parallel and Concurrent Programming Part 1

Unlock the full course today

Join today to access over 22,700 courses taught by industry experts or purchase this course individually.

Read-write lock: Python demo

Read-write lock: Python demo - Python Tutorial

From the course: Python Parallel and Concurrent Programming Part 1

Start my 1-month free trial

Read-write lock: Python demo

- Reader-writer locks are a common feature in many programming languages that support concurrency, however they're not included by default in Python. When I need something that isn't standard in Python, my first stop is always the Python package index at pypi.org and sure enough, someone has already implemented a reader-writer lock package for python. I'll install it for this demonstration using the Python package manager with the console command pip install reader writer lock. And it's that easy. In this example program, we create 10 threads to concurrently read what day it is from a shared calendar while two other threads update it. The calendar here is just a list of strings to represent the seven days of the week on line six, as well as an integer to indicate which day today is on line seven. I've created a single regular lock object named marker on line eight which all of the threads will use to enforce mutual…

Contents