Making Threads Work Together — Synchronization
Picture a kitchen in India with two cooks, Gautam and Badal. They’re both preparing delicious dishes, but there’s a unique spice box they both want to use. However, only one cook can use it at a time.
Here’s how they ensure a flavorful collaboration:
Diverse Tasks:
- Gautam is making biryani, and Badal is crafting curry. Both tasks require a special spice box.
Single Spice Box:
- There’s only one spice box, and they need to share it.
Alternating Turns:
- They decide to take turns. If Gautam uses the spice box, Badal patiently waits, and vice versa.
Effective Communication:
- Gautam and Badal communicate well. When one finishes, they inform the other, preventing any spice-related chaos.
Harmonious Cooking:
- This synchronized approach creates a feast without overwhelming the spice box. It’s like a coordinated dance in the lively kitchen of flavors.
This scenario mirrors thread synchronization in the programming world, particularly with threads. Threads, like Gautam and Badal, work together, taking turns to access shared resources and ensuring a harmonious process.
Let’s talk about how threads can play nicely together in Python. It’s like teaching them to take turns to work together without causing a mess.
Basic knowledge of multi-threading will make you a quick learner. In Python, there’s a cool module called threading.
It’s like a teamwork kit for your code, making things work together smoothly.
Let’s talk about taking turns first
The threading module in Python has four special classes that help your threads play fair and take turns. It’s like having rules to ensure everyone gets a chance to do their job without any chaos.
Lock
It is like a key that threads use to access a shared resource one at a time. It prevents conflicts by allowing only one Thread to hold the lock, ensuring the orderly execution of critical code sections.
Let’s say Gautam is using a knife to chop something so Badal has to wait till Gautam releases the knife.
“Lock” allows only one thread to acquire the lock at a time.
If a thread holds the lock and tries to acquire it again (nested acquisition), it will be deadlocked. In other words, it doesn’t allow the same thread to acquire the lock multiple times. It’s simple and suitable for basic synchronization needs.
RLock (Re-entrant/Recursive Lock)
It is much like Lock
and works like a key for thread to access a resource at a time. The only difference between the same Lock
and RLock
is It can be acquired n times and released n times by the same thread, but to get it acquired by another thread the, the acquire and release count should be the same.
The following code will create DeadLock
Since the same lock is getting acquired multiple times by the same thread.
To overcome this DeadLock
the situation we can go for RLock
The RLock
will allow the same lock to be acquired by the same thread and DeadLock
the situation will be ignored.
Semaphore
Semaphore is used to limit the number of threads that can access a resource at the same time. Works same as Lock
Summary
The Lock, RLock, and Semaphore classes enable you to make multiple threads operate on the same resource more efficiently. All three classes control the behavior of a thread over resources and can save you from inconsistency.
Official Website https://pythonbricks.pythonanywhere.com/
Youtube Channel https://www.youtube.com/@PythonBricks
Telegram https://t.me/pythonbricks
Watch and Learn: https://www.youtube.chttps://www.youtube.com/watch?v=Ke8gBPq9emsom