def flock(path):
"""Attempt to acquire a POSIX file lock.
"""
with open(path, "w+") as lf:
try:
fcntl.flock(lf, fcntl.LOCK_EX | fcntl.LOCK_NB)
acquired = True
yield acquired
except OSError:
acquired = False
yield acquired
finally:
if acquired:
fcntl.flock(lf, fcntl.LOCK_UN)
评论列表
文章目录