If multiple threads are executing simultaneously then there may be a chance of data inconsistency problems.
Eg:
1) from threading import * 2) import time 3) def wish(name): 4) for i in range(10): 5) print("Good Evening:",end='') 6) time.sleep(2) 7) print(name) 8) t1=Thread(target=wish,args=("Dhoni",)) 9) t2=Thread(target=wish,args=("Yuvraj",)) 10) t1.start() 11) t2.start()
Output:
Good Evening:Good Evening:Yuvraj Dhoni Good Evening:Good Evening:Yuvraj Dhoni
....
We are getting irregular output b'z both threads are executing simultaneously wish() function.
To overcome this problem we should go for synchronization.
Synchronization By using Lock concept:
Locks are the most fundamental synchronization mechanism provided by threading module.
We can create Lock object as follows
l=Lock()
The Lock object can be hold by only one thread at a time.If any other thread required the same lock then it will wait until thread releases lock.(similar to common wash rooms,public telephone booth etc)
A Thread can acquire the lock by using acquire() method.
l.acquire()
A Thread can release the lock by using release() method.
l.release()
Note: To call release() method compulsory thread should be owner of that lock.i.e thread should has the lock already,otherwise we will get Runtime Exception saying
RuntimeError: release unlocked lock
Eg:
1) from threading import * 2) l=Lock() 3) #l.acquire() ==>1 4) l.release()
If we are commenting line-1 then we will get
RuntimeError: release unlocked lock
Problem with Simple Lock:
The standard Lock object does not care which thread is currently holding that lock.If the lock is held and any thread attempts to acquire lock, then it will be blocked,even the same thread is already holding that lock.
Eg:
1) from threading import * 2) l=Lock() 3) print("Main Thread trying to acquire Lock") 4) l.acquire() 5) print("Main Thread trying to acquire Lock Again") 6) l.acquire()
Output:
D:\python_classes>py test.py Main Thread trying to acquire Lock Main Thread trying to acquire Lock Again
Difference between Lock and RLock:
Lock:
1. Lock object can be acquired by only one thread at a time.Even owner thread also cannot acquire multiple times.
2. Not suitable to execute recursive functions and nested access calls
3. In this case Lock object will takes care only Locked or unlocked and it never takes care about owner thread and recursion level.
RLock:
1. RLock object can be acquired by only one thread at a time, but owner thread can acquire same lock object multiple times.
2. Best suitable to execute recursive functions and nested access calls
3. In this case RLock object will takes care whether Locked or unlocked and owner thread information, recursiion level.
Synchronization by using Semaphore:
In the case of Lock and RLock,at a time only one thread is allowed to execute.
Sometimes our requirement is at a time a particular number of threads are allowed to access(like at a time 10 memebers are allowed to access database server,4 members are allowed to access Network connection etc).To handle this requirement we cannot use Lock and RLock concepts and
we should go for Semaphore concept.
Semaphore can be used to limit the access to the shared resources with limited capacity.
Semaphore is advanced Synchronization Mechanism.
BoundedSemaphore:
Normal Semaphore is an unlimited semaphore which allows us to call release() method any number of times to increment counter.The number of release() calls can exceed the number of acquire() calls also.
Eg:
1) from threading import * 2) s=Semaphore(2) 3) s.acquire() 4) s.acquire() 5) s.release() 6) s.release() 7) s.release() 8) s.release() 9) print("End")
It is valid because in normal semaphore we can call release() any number of times.
BounedSemaphore is exactly same as Semaphore except that the number of release() calls should not exceed the number of acquire() calls,otherwise we will get
ValueError: Semaphore released too many times
"Python Synchronization"
"Python Synchronization
Primitives"
"Python Synchronization Between
Processes"
"Python Synchronization Library"
"Python Process Synchronization"
"Multiprocessing Python
Synchronization"
"Semaphore In Python
Synchronization"
"Python Thread Synchronization"
"Python Multiprocessing
Synchronization"
"Python Monitor Synchronization"