jztools.filelock#
Fcntl / flock Unix advisory file locking with support for exclusive and shared locks.
Classes
|
Behaves like a |
|
Fcntl/flock file locking. |
|
- class jztools.filelock.FileLock(filename, mode='exclusive', poll_interval=0.02)#
Bases:
objectFcntl/flock file locking. Shared locks can be re-acquired from any
FileLockobject or process. Exclusive locks can only be re-acquired from the sameFileLockobject and process. Note in particular that this object does not enforce exclusive locks between threads for the same lock object (useCompoundFileLockfor this purpose).If a file is moved while a lock is active, that lock will continue to be valid on the moved file. If it is deleted, however, that lock will continue to behave like the file existed, but it will not lock against a newly created file of the same name.
Lock acquisition for exclusive locks can implicitly upgrade or downgrade the lock:
Mode ‘exclusive’ can operate in ‘shared’ and ‘exclusive’ mode, with ‘exclusive’ being sticky: None -> acquire(mode=’shared’) -> ‘shared’; acquire(mode=’exclusive’) -> ‘exclusive’; acquire(mode=’shared’) -> ‘exclusive’; release() -> ‘exclusive’; release() -> ‘shared’; release() -> None
Mode ‘shared’ will result in an error if ‘exclusive’ is requested.: acquire(mode=’shared’) -> ‘shared’, acquire(mode=’exclusive’) -> Exception
- Parameters:
filename – Name of file to apply advisory lock to.
mode – The maximum supported mode - one of
'shared'(or its alias'read'), or'exclusive'(or its alias'write').
- downgrade_max_mode()#
Ensures that the max mode is shared mode.
- with_acquire(**acquire_kwargs)#
Context manager that fails if the lock type is not of the specified type, or if the acquire call times out.
- acquire(timeout=-1, create=False, mode=None)#
- Parameters:
create – Will create the lock file if mode is ‘write’/’exclusive’.
- class jztools.filelock.CompoundFileLock(file_lock, *locks)#
Bases:
FileLockBehaves like a
FileLockobject but acquires/releases one or more locks (assumed to have thethreading.Lockinterface) simultaneously.- Parameters:
filename – Name of file to apply advisory lock to.
mode – The maximum supported mode - one of
'shared'(or its alias'read'), or'exclusive'(or its alias'write').
- acquire(timeout=-1, **kwargs)#
- Parameters:
create – Will create the lock file if mode is ‘write’/’exclusive’.
- with_acquire(timeout=-1, **kwargs)#
Context manager that fails if the lock type is not of the specified type, or if the acquire call times out.