import threading
a_lock = threading.Lock()
b_lock = threading.Lock()
def foo():
with a_lock:
print("a locked")
with b_lock:
print("now b locked")
def bar():
with b_lock:
print("different locking order")
with a_lock:
print("possible deadlock")
while True:
t1 = threading.Thread(target=foo)
t2 = threading.Thread(target=bar)
t1.start()
t2.start()