February 2019
Intermediate to advanced
672 pages
16h 50m
English
In Chapter 19, Deadlocks, we covered an interesting phenomenon, in which the use of locks can lead to undesirable results. Specifically, we found out that, with enough locks implemented in a concurrent program, the whole program can become sequential. Let's analyze this concept with our current program. Consider the Chapter21/example3.py file, as follows:
# ch21/example3.pyimport threadingimport random; random.seed(0)import timedef update(pause_period): global counter with count_lock: current_counter = counter # reading in shared resource time.sleep(pause_period) # simulating heavy calculations counter = current_counter + 1 # updating shared resourcepause_periods = [random.randint(0, 1) for i in range(20)]########################################################################### ...