Simple file locking for Unix using fcntl
| 60 | import errno |
| 61 | |
| 62 | class UnixFileLock(BaseLock): |
| 63 | """Simple file locking for Unix using fcntl""" |
| 64 | |
| 65 | def __init__(self, fileobj, mode: int = 0) -> None: |
| 66 | super().__init__() |
| 67 | self.fileobj = fileobj |
| 68 | self.mode = mode | fcntl.LOCK_EX |
| 69 | |
| 70 | def acquire(self) -> None: |
| 71 | try: |
| 72 | fcntl.flock(self.fileobj, self.mode) |
| 73 | self.locked = True |
| 74 | except OSError as e: |
| 75 | if e.errno != errno.ENOLCK: |
| 76 | raise e |
| 77 | |
| 78 | def release(self) -> None: |
| 79 | self.locked = False |
| 80 | fcntl.flock(self.fileobj, fcntl.LOCK_UN) |
| 81 | |
| 82 | has_fcntl = True |
| 83 | except ImportError: |