Create a file and configure it to be locking.
(path: str)
| 48 | |
| 49 | @contextmanager |
| 50 | def unix_flock(path: str): |
| 51 | """Create a file and configure it to be locking.""" |
| 52 | fd: int | None = None |
| 53 | |
| 54 | try: |
| 55 | fd = os.open(path, os.O_CREAT | os.O_WRONLY, 0o644) |
| 56 | # See https://man7.org/linux/man-pages/man2/flock.2.html |
| 57 | flock(fd, LOCK_EX) |
| 58 | yield fd |
| 59 | finally: |
| 60 | if fd is not None: |
| 61 | flock(fd, LOCK_UN) |
| 62 | os.close(fd) |
| 63 | |
| 64 | |
| 65 | @contextmanager |
no outgoing calls
no test coverage detected