First page Back Continue Last page Overview Graphics

Rlock example

If we exchange

lock = threading.RLock()

with

lock = threading.Lock()

the function a() can't acquire the lock again, because ab() has already acquired it!

import threading

lock = threading.RLock()

def a():

with lock:

print("Executing a")

return "a"

def b():

with lock:

print("Executing b")

return "b"

def ab():

with lock:

first = a()

second =b()

return first, second

print(ab())

Output:

a: Do what has to be done

b: Do what has to be done

('a', 'b')