O'Reilly logo

Tkinter GUI Application Development Blueprints - Second Edition by Bhaskar Chaudhary

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using synchronization primitives

To handle this complexity, the threading module provides some synchronization primitives, such as locks, joins, semaphores, events, and condition variables.

9.02_lock_demo.py slightly modifies the preceding code by introducing a lock using this line:

self.lock = threading.Lock()

Next, every time shared_variable is to be modified, it is done after acquiring a lock. The lock is released when the variable has been modified, as shown in the following code:

self.lock.acquire()self.shared_var += 1self.lock.release()

This enables us to avoid a race condition. Since this code operates with a lock, it produces no change in the shared variable after an equal number of increments and decrements.

It seemed easy to use ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required