portalocker package¶
Submodules¶
- portalocker.redis module
- portalocker.constants module
LOCK_EX
LOCK_NB
LOCK_SH
LOCK_UN
LockFlags
LockFlags.EXCLUSIVE
LockFlags.NON_BLOCKING
LockFlags.SHARED
LockFlags.UNBLOCK
LockFlags.__abs__()
LockFlags.__add__()
LockFlags.__and__()
LockFlags.__bool__()
LockFlags.__ceil__()
LockFlags.__contains__()
LockFlags.__dir__()
LockFlags.__divmod__()
LockFlags.__eq__()
LockFlags.__float__()
LockFlags.__floor__()
LockFlags.__floordiv__()
LockFlags.__format__()
LockFlags.__ge__()
LockFlags.__getattribute__()
LockFlags.__getnewargs__()
LockFlags.__gt__()
LockFlags.__hash__()
LockFlags.__index__()
LockFlags.__init__()
LockFlags.__int__()
LockFlags.__invert__()
LockFlags.__iter__()
LockFlags.__le__()
LockFlags.__len__()
LockFlags.__lshift__()
LockFlags.__lt__()
LockFlags.__mod__()
LockFlags.__module__
LockFlags.__mul__()
LockFlags.__ne__()
LockFlags.__neg__()
LockFlags.__new__()
LockFlags.__or__()
LockFlags.__pos__()
LockFlags.__pow__()
LockFlags.__radd__()
LockFlags.__rand__()
LockFlags.__rdivmod__()
LockFlags.__reduce_ex__()
LockFlags.__repr__()
LockFlags.__rfloordiv__()
LockFlags.__rlshift__()
LockFlags.__rmod__()
LockFlags.__rmul__()
LockFlags.__ror__()
LockFlags.__round__()
LockFlags.__rpow__()
LockFlags.__rrshift__()
LockFlags.__rshift__()
LockFlags.__rsub__()
LockFlags.__rtruediv__()
LockFlags.__rxor__()
LockFlags.__sizeof__()
LockFlags.__str__()
LockFlags.__sub__()
LockFlags.__truediv__()
LockFlags.__trunc__()
LockFlags.__xor__()
LockFlags._iter_member_()
LockFlags.as_integer_ratio()
LockFlags.bit_count()
LockFlags.bit_length()
LockFlags.conjugate()
LockFlags.denominator
LockFlags.from_bytes()
LockFlags.imag
LockFlags.is_integer()
LockFlags.numerator
LockFlags.real
LockFlags.to_bytes()
- portalocker.exceptions module
- portalocker.portalocker module
- portalocker.utils module
Module contents¶
- exception portalocker.AlreadyLocked(*args: Any, fh: IO | None = None, **kwargs: Any)[source]¶
Bases:
LockException
- class portalocker.BoundedSemaphore(maximum: int, name: str = 'bounded_semaphore', filename_pattern: str = '{name}.{number:02d}.lock', directory: str = '/usr/src/tmp', timeout: float | None = 5, check_interval: float | None = 0.25, fail_when_locked: bool | None = True)[source]¶
Bases:
LockBase
Bounded semaphore to prevent too many parallel processes from running
It’s also possible to specify a timeout when acquiring the lock to wait for a resource to become available. This is very similar to threading.BoundedSemaphore but works across multiple processes and across multiple operating systems.
>>> semaphore = BoundedSemaphore(2, directory='') >>> str(semaphore.get_filenames()[0]) 'bounded_semaphore.00.lock' >>> str(sorted(semaphore.get_random_filenames())[1]) 'bounded_semaphore.01.lock'
- portalocker.LOCK_EX: LockFlags = LockFlags.EXCLUSIVE¶
Place an exclusive lock. Only one process may hold an exclusive lock for a given file at a given time.
- portalocker.LOCK_NB: LockFlags = LockFlags.NON_BLOCKING¶
Acquire the lock in a non-blocking fashion.
- portalocker.LOCK_SH: LockFlags = LockFlags.SHARED¶
Place a shared lock. More than one process may hold a shared lock for a given file at a given time.
- class portalocker.Lock(filename: str | Path, mode: str = 'a', timeout: float = None, check_interval: float = 0.25, fail_when_locked: bool = False, flags: LockFlags = LockFlags.EXCLUSIVE | NON_BLOCKING, **file_open_kwargs)[source]¶
Bases:
LockBase
Lock manager with built-in timeout
- Parameters:
filename – filename
mode – the open mode, ‘a’ or ‘ab’ should be used for writing. When mode contains
w
the file will be truncated to 0 bytes.timeout – timeout when trying to acquire a lock
check_interval – check interval while waiting
fail_when_locked – after the initial lock failed, return an error or lock the file. This does not wait for the timeout.
**file_open_kwargs – The kwargs for the
open(...)
call
fail_when_locked is useful when multiple threads/processes can race when creating a file. If set to true than the system will wait till the lock was acquired and then return an AlreadyLocked exception.
Note that the file is opened first and locked later. So using ‘w’ as mode will result in truncate _BEFORE_ the lock is checked.
- exception portalocker.LockException(*args: Any, fh: IO | None = None, **kwargs: Any)[source]¶
Bases:
BaseLockException
- class portalocker.LockFlags(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]¶
Bases:
IntFlag
- EXCLUSIVE = 2¶
exclusive lock
- NON_BLOCKING = 4¶
non-blocking
- SHARED = 1¶
shared lock
- UNBLOCK = 8¶
unlock
- class portalocker.RLock(filename, mode='a', timeout=5, check_interval=0.25, fail_when_locked=False, flags=LockFlags.EXCLUSIVE | NON_BLOCKING)[source]¶
Bases:
Lock
A reentrant lock, functions in a similar way to threading.RLock in that it can be acquired multiple times. When the corresponding number of release() calls are made the lock will finally release the underlying file lock.
- portalocker.lock(file_: IO, flags: LockFlags)[source]¶
Lock a file. Note that this is an advisory lock on Linux/Unix systems
- portalocker.open_atomic(filename: str | Path, binary: bool = True) Iterator[IO] [source]¶
Open a file for atomic writing. Instead of locking this method allows you to write the entire file and move it to the actual location. Note that this makes the assumption that a rename is atomic on your platform which is generally the case but not a guarantee.
http://docs.python.org/library/os.html#os.rename
>>> filename = 'test_file.txt' >>> if os.path.exists(filename): ... os.remove(filename)
>>> with open_atomic(filename) as fh: ... written = fh.write(b'test') >>> assert os.path.exists(filename) >>> os.remove(filename)
>>> import pathlib >>> path_filename = pathlib.Path('test_file.txt')
>>> with open_atomic(path_filename) as fh: ... written = fh.write(b'test') >>> assert path_filename.exists() >>> path_filename.unlink()