Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use event/condition/lock/semaphore in python's threading module?

Python provides 4 different synchronizing mechanisms in threading module: Event/Condition/Lock(RLock)/Semaphore.

I understand they can be used to synchronize access of shared resources/critical sections between threads. But I am not quite sure when to use which.

Can they be used interchangeably? Or are some of them 'higher level', using others as building blocks? If so, which ones are built on which?

It would be great if someone can illustrate with some examples.

like image 667
NeoWang Avatar asked Jul 27 '15 03:07

NeoWang


People also ask

What is the purpose of threading lock () in Python?

Locks help us in efficiently accessing a shared resource in a program in order to prevent corruption of data, it follows mutual exclusion as only one thread can access a particular resource at a time.

What is the difference between threading lock and threading RLock?

Lock can only be acquired once, and once acquired it cannot be acquired again by the same thread or any other thread until it has been released. A threading. RLock can be acquired more than once by the same thread, although once acquired by a thread it cannot be acquired by a different thread until it is been released.

What is the difference between a semaphore and bounded semaphore?

A Semaphore can be released more times than it's acquired, and that will raise its counter above the starting value. A BoundedSemaphore can't be raised above the starting value.

How does semaphore work in Python?

Semaphore ObjectsA semaphore manages an internal counter which is decremented by each acquire() call and incremented by each release() call. The counter can never go below zero; when acquire() finds that it is zero, it blocks, waiting until some other thread calls release() .


1 Answers

This article probably contains all the information you need. The question is indeed very broad, but let me try to explain how I use each as an example:

  1. Event - Use it when you need threads to communicate a certain state was met so they can both work together in sync. I use it mostly for initiation process of two threads where one dependes on the other.

    Example: A client has a threaded manager, and its __init__() needs to know the manager is done instantiating some attributes before it can move on.

  2. Lock/RLock - Use it when you are working with a shared resource and you want to make sure no other thread is reading/writing to it. Although I'd argue that while locking before writing is mandatory, locking before reading could be optional. But it is good to make sure that while you are reading/writing, no other thread is modifying it at the same time. RLock has the ability to be acquired multiple times by its owner, and release() must be called the same amount of times acquire() was in order for it to be used by another thread trying to acquire it.

I haven't used Condition that much, and frankly never had to use Semaphore, so this answer has room of editing and improvement.

like image 152
sargas Avatar answered Nov 02 '22 09:11

sargas