Custom Search

Tuesday, November 23, 2010

Python Object Level Lock and Synchronized Method

==================================

* Object level lock.
* Python Synchronized method.
* Here variable 'lock' is an instance variable.
* So each object has its own instance variable 'lock'.
* Suppose object 'a' acquire a lock on function 'first_function', this time
object 'b' can call and acquire lock on function 'first_method', without out waiting for
object 'a' to release the lock.


import threading
class A:

def __init__(self):
#For private variable use single underscore before variable name.
#self._lock = threading.RLock()
self.lock = threading.RLock()

def first_function(self, name):
print "------first_function, Waiting for acquire lock------", name
self.lock.acquire()
try:
print "------first_function, lock acquired------", name
print "------first_function, sleeping----start----", name
import time
time.sleep(5)
print "------first_function, sleeping----end------", name
finally:
self.lock.release()
print "------ first_function, lock released-------", name

def second_function(self, name):
print "------second_function, Waiting for acquire lock------", name
self.lock.acquire()
try:
print "------second_function, lock acquired------", name
print "------second_function, sleeping----start----", name
import time
time.sleep(5)
print "------second_function, sleeping----end------", name
finally:
self.lock.release()
print "------second_function, lock released-------", name

a = A()

thr1 = threading.Thread(name='a.first_function',target=a.first_function, args=('T1',))
thr2 = threading.Thread(name='a.first_function', target=a.first_function, args=('T2',))

print "----------calling------thr1.start()------"
thr1.start()
print "----------calling------thr2.start()------"
thr2.start()

----------------------------------------------------CASE-1

2 threads 'thr1' and 'thr2' trying to call a method 'first_function' of class 'A'
using same object 'a'.

a = A()
thr1 = threading.Thread(name='a.first_function',target=a.first_function, args=('T1',))
thr2 = threading.Thread(name='a.first_function', target=a.first_function, args=('T2',))

OUTPUT
======
----------calling------thr1.start()------
----------calling------thr2.start()------
------first_function, Waiting for acquire lock------ T1
------first_function, lock acquired------ T1
------first_function, sleeping----start---- T1
------first_function, Waiting for acquire lock------ T2
------first_function, sleeping----end------ T1
------ first_function, lock released------- T1
------first_function, lock acquired------ T2
------first_function, sleeping----start---- T2
------first_function, sleeping----end------ T2
------ first_function, lock released------- T2


----------------------------------------------------CASE-2

2 threads 'thr1' and 'thr2' trying to call two seperate methods 'first_function'
and 'second_function' of class 'A' using same object 'a'.

a = A()

thr1 = threading.Thread(name='a.first_function',target=a.first_function, args=('T1',))
thr2 = threading.Thread(name='a.second_function', target=a.second_function, args=('T2',))

OUTPUT
======
----------calling------thr1.start()------
----------calling------thr2.start()------
------first_function, Waiting for acquire lock------ T1
------first_function, lock acquired------ T1
------first_function, sleeping----start---- T1
------second_function, Waiting for acquire lock------ T2
------first_function, sleeping----end------ T1
------ first_function, lock released------- T1
------second_function, lock acquired------ T2
------second_function, sleeping----start---- T2
------second_function, sleeping----end------ T2
------second_function, lock released------- T2


----------------------------------------------------CASE-3

2 threads 'thr1' and 'thr2' trying to call a method 'first_function' of class 'A'
using different objects 'a' and 'b'.

a = A()
b = A()

thr1 = threading.Thread(name='a.first_function',target=a.first_function, args=('T1',))
thr2 = threading.Thread(name='b.first_function', target=b.first_function, args=('T2',))

OUTPUT
======
----------calling------thr1.start()------
------first_function, Waiting for acquire lock------ T1
------first_function, lock acquired------ T1
----------calling------thr2.start()------
------first_function, sleeping----start---- T1
------first_function, Waiting for acquire lock------ T2

------first_function, lock acquired------ T2
------first_function, sleeping----start---- T2
------first_function, sleeping----end------ T1
------ first_function, lock released------- T1
------first_function, sleeping----end------ T2
------ first_function, lock released------- T2


----------------------------------------------------CASE-4

2 threads 'thr1' and 'thr2' trying to call two seperate methods 'first_function' and
'second_function' of class 'A' using two seperate objects 'a' and 'b'.

a = A()
b = A()

thr1 = threading.Thread(name='a.first_function',target=a.first_function, args=('T1',))
thr2 = threading.Thread(name='b.second_function', target=b.second_function, args=('T2',))

OUTPUT
======
----------calling------thr1.start()------
------first_function, Waiting for acquire lock------ T1
----------calling------thr2.start()------
------first_function, lock acquired------ T1
------first_function, sleeping----start---- T1
------second_function, Waiting for acquire lock------ T2
------second_function, lock acquired------ T2
------second_function, sleeping----start---- T2
------first_function, sleeping----end------ T1
------ first_function, lock released------- T1
------second_function, sleeping----end------ T2
------second_function, lock released------- T2



==================================

No comments:

Post a Comment