Serialize cooperating backend instances, including separate processes.
Each acquisition opens its own descriptor. On Unix, flock associates ownership
with that open file description, so independent threads also exclude one another.
The lock file must remain in place: unlinking it would allow a new caller to lock
a different inode while another process still owns the original lock.
Callers must not recursively acquire this lock.
Source code in dynamiq/checkpoints/backends/_filesystem_lock.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53 | @contextmanager
def directory_lock(directory: Path) -> Iterator[None]:
"""Serialize cooperating backend instances, including separate processes.
Each acquisition opens its own descriptor. On Unix, flock associates ownership
with that open file description, so independent threads also exclude one another.
The lock file must remain in place: unlinking it would allow a new caller to lock
a different inode while another process still owns the original lock.
Callers must not recursively acquire this lock.
"""
with (directory / ".checkpoint.lock").open("a+b") as lock_file:
if os.name == "nt":
# Windows byte-range locks require a byte to lock, including on an empty store.
lock_file.seek(0, os.SEEK_END)
if lock_file.tell() == 0:
lock_file.write(b"\0")
lock_file.flush()
lock_file.seek(0)
while True:
try:
msvcrt.locking(lock_file.fileno(), msvcrt.LK_NBLCK, 1)
break
except OSError as exc:
if exc.errno not in (errno.EACCES, errno.EAGAIN, errno.EDEADLK):
raise
time.sleep(0.05)
else:
fcntl.flock(lock_file.fileno(), fcntl.LOCK_EX)
try:
yield
finally:
if os.name == "nt":
lock_file.seek(0)
msvcrt.locking(lock_file.fileno(), msvcrt.LK_UNLCK, 1)
else:
fcntl.flock(lock_file.fileno(), fcntl.LOCK_UN)
|