我知道RLock在发布之前可以多次被收购。我的问题是在以下情况下,我可以使用多个锁而不是使用RLock。我跑了它,它按照我的期望工作正常。
import threading
##declare two locks, is that good way to do like this or using RLock
outer_lock = threading.Lock()
lock = threading.Lock()
t = []
def get_first_part():
lock.acquire()
global t
try:
t.append('first')
finally:
lock.release()
return t
def get_second_part():
lock.acquire()
global t
try:
t.append('second')
finally:
lock.release()
return t
def get_both_part():
outer_lock.acquire()
try:
first = get_first_part()
second = get_second_part()
finally:
outer_lock.release()
return first, second
t1 = threading.Thread(target=get_both_part)
t2 = threading.Thread(target=get_both_part)
t1.start()
t2.start()
t1.join()
t2.join()