open_with_lock¶
- open_with_lock(filename: str, mode: str = 'r', *args: Any, **kwargs: Any) Iterator[TextIOWrapper][source]¶
Open a file with an exclusive lock.
This context manager attempts to acquire an exclusive lock on the file upon opening. Other processes using open_with_lock will wait until the lock is automatically released when exiting the context.
- Parameters:
filename -- Path to the file to open and lock.
mode -- File open mode (e.g., 'rt', 'wt', 'a', 'rb').
*args -- Additional positional arguments to the built-in open() function.
**kwargs -- Additional keyword arguments to the built-in open() function.
- Yields:
The opened file object with an exclusive lock acquired (if supported by the filesystem).
Notes
All file open calls with open_with_lock will block other open_with_lock usages (even from different processes/Lustre clients) from read/write until the lock is released. However, this locking/blocking mechanism does not prevent file access (which could potentially cause IO errors) from other file openers (e.g., the built-in open() function).
Filesystem Support: The Python fcntl API's behavior depends on the underlying OS/storage implementation: https://docs.python.org/3/library/fcntl.html. Not all OS/file systems fully support file locking. - Lustre: Requires mount option -o flock. Check with: mount -l | grep lustre
e.g. : 192.168.1.30@o2ib:/aoclst03 on /.lustre/aoc type lustre (rw,flock,user_xattr,lazystatfs)
NFS: Support varies by configuration (see PIPE-2051 for details)
Local filesystems: Generally well-supported on Unix-like systems
Testing Lock Behavior: To verify exclusive locking works on your system, run this from multiple processes:
```python import fcntl with open(filename, 'w') as fd:
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB) # Should block/fail on second process