6
class LockError(Exception):
7
"""Raised when unable to lock a file."""
10
def lock_path(path, timeout=0):
11
fd = os.open(path, os.O_CREAT)
12
flags = fcntl.fcntl(fd, fcntl.F_GETFD, 0)
13
flags |= fcntl.FD_CLOEXEC
14
fcntl.fcntl(fd, fcntl.F_SETFD, flags)
20
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
22
if started < time.time() - timeout:
23
raise LockError("Couldn't obtain lock")
29
fcntl.flock(fd, fcntl.LOCK_UN)