def with_multi_lock(tag, n, unlock_after_with=True):
get_lock_file_path = lambda i: os.path.join(
'/tmp/', tag + '.lock' + (str(i) if i else ''))
for i in range(n):
lock_file_path = get_lock_file_path(i)
fd = os.open(lock_file_path, os.O_CREAT | os.O_RDWR, 0660)
try:
if trylock(fd):
yield True
break
finally:
if unlock_after_with:
os.close(fd)
else:
yield False
评论列表
文章目录