holypipette.utils.filelock module¶
A platform independent file lock that supports the with-statement.
- class holypipette.utils.filelock.BaseFileLock(lock_file, timeout=- 1)[source]¶
Bases:
object
Implements the base class of a file lock.
- acquire(timeout=None, poll_intervall=0.05)[source]¶
Acquires the file lock or fails with a
Timeout
error.# You can use this method in the context manager (recommended) with lock.acquire(): pass # Or use an equivalent try-finally construct: lock.acquire() try: pass finally: lock.release()
- Parameters:
timeout (float) – The maximum time waited for the file lock. If
timeout < 0
, there is no timeout and this method will block until the lock could be acquired. Iftimeout
is None, the defaulttimeout
is used.poll_intervall (float) – We check once in poll_intervall seconds if we can acquire the file lock.
- Raises:
Timeout – if the lock could not be acquired in timeout seconds.
Changed in version 2.0.0: This method returns now a proxy object instead of self, so that it can be used in a with statement without side effects.
- property is_locked¶
True, if the object holds the file lock.
Changed in version 2.0.0: This was previously a method and is now a property.
- property lock_file¶
The path to the lock file.
- release(force=False)[source]¶
Releases the file lock.
Please note, that the lock is only completly released, if the lock counter is 0.
Also note, that the lock file itself is not automatically deleted.
- Parameters:
force (bool) – If true, the lock counter is ignored and the lock is released in every case.
- property timeout¶
You can set a default timeout for the filelock. It will be used as fallback value in the acquire method, if no timeout value (None) is given.
If you want to disable the timeout, set it to a negative value.
A timeout of 0 means, that there is exactly one attempt to acquire the file lock.
New in version 2.0.0.
- holypipette.utils.filelock.FileLock¶
Alias for the lock, which should be used for the current platform. On Windows, this is an alias for
WindowsFileLock
, on Unix forUnixFileLock
and otherwise forSoftFileLock
.
- class holypipette.utils.filelock.SoftFileLock(lock_file, timeout=- 1)[source]¶
Bases:
BaseFileLock
Simply watches the existence of the lock file.
- exception holypipette.utils.filelock.Timeout(lock_file)[source]¶
Bases:
TimeoutError
Raised when the lock could not be acquired in timeout seconds.
- lock_file¶
The path of the file lock.
- class holypipette.utils.filelock.UnixFileLock(lock_file, timeout=- 1)[source]¶
Bases:
BaseFileLock
Uses the
fcntl.flock()
to hard lock the lock file on unix systems.
- class holypipette.utils.filelock.WindowsFileLock(lock_file, timeout=- 1)[source]¶
Bases:
BaseFileLock
Uses the
msvcrt.locking()
function to hard lock the lock file on windows systems.