假设我有以下两个功能。许多线程可以使用第一个(use_stuff
)进行一些计算,而当我们需要更新缓存时,可以在单独的线程中偶尔调用第二个(clean_stuff_cache
)。我应该使用哪种同步机制(我在这里称之为counter
)来确保clean_stuff_cache
将阻塞,直到所有线程完成use_stuff
例程。
stuff_cache = dict()
counter = ????
def use_stuff(id):
counter.acquire()
if id not in stuff_cache:
stuff_cache[id] = get_stuff(id)
# do some computation using stuff
do_stuff(stuff_cache[id])
counter.release()
def clean_stuff_cache(id):
counter.wait_until_zero()
# drop all the stuff being used
del stuff[id]
答案 0 :(得分:1)
您可以在此处使用Semaphore
对象与Lock
结合使用。信号量允许有一个同步计数器,如果你试图获取它是否为0时阻止该计数器
因此,如果counter = Semaphore(0)
和lock = Lock()
则:
def use_stuff(id):
# Increment number of running workers if your are not cleaning the cache
with lock:
counter.release()
..... do stuff...
# decrement number of running workers
counter.acquire()
def clean_stuff():
# counter.acquire return False if there is no worker running
while True:
lock.acquire()
if counter.acquire(False):
counter.release()
lock.release()
else:
break
# delay next loop so the check is only occasional
time.sleep(.01)
# here, no worker is running and the lock prevents new worker
# from starting so you can cleanup the cache...
... do clean up
lock.release()