clan-core/pkgs/clan-cli/clan_cli/locked_open.py

16 lines
642 B
Python
Raw Normal View History

2023-12-01 07:00:15 +00:00
import fcntl
from collections.abc import Generator
from contextlib import contextmanager
from pathlib import Path
@contextmanager
def locked_open(filename: str | Path, mode: str = "r") -> Generator:
"""
This is a context manager that provides an advisory write lock on the file specified by `filename` when entering the context, and releases the lock when leaving the context. The lock is acquired using the `fcntl` module's `LOCK_EX` flag, which applies an exclusive write lock to the file.
"""
with open(filename, mode) as fd:
fcntl.flock(fd, fcntl.LOCK_EX)
yield fd
fcntl.flock(fd, fcntl.LOCK_UN)