portalocker.constants module

Locking constants.

Lock types:

  • EXCLUSIVE exclusive lock

  • SHARED shared lock. Note: on Windows this requires the optional pywin32 package (pip install "portalocker[win32]"); without it, acquiring a shared lock raises ImportError.

Lock flags:

  • NON_BLOCKING non-blocking

Manually unlock, only needed internally

  • UNBLOCK unlock

portalocker.constants.LOCK_EX = 2

exclusive lock

portalocker.constants.LOCK_NB = 4

non-blocking

portalocker.constants.LOCK_SH = 1

shared lock

portalocker.constants.LOCK_UN = 8

unlock

class portalocker.constants.LockFlags(*values)[source]

Bases: IntFlag

Flags selecting how portalocker.lock acquires a file lock.

Members combine with the bitwise | operator to build up the behavior you want. The library’s own default, used when no flags argument is given, is LockFlags.EXCLUSIVE | LockFlags.NON_BLOCKING: take an exclusive lock and fail immediately rather than wait.

On POSIX platforms these map onto fcntl locks, which are advisory: the OS only enforces them against other processes that also lock the file through fcntl/flock. A process that opens and writes the file without locking it is never blocked. On Windows they map onto msvcrt locks, except SHARED, which requires the optional pywin32 package (pip install "portalocker[win32]"); without it, acquiring a shared lock raises ImportError.

Example

>>> import portalocker
>>> flags = (
...     portalocker.LockFlags.EXCLUSIVE
...     | portalocker.LockFlags.NON_BLOCKING
... )
>>> bool(flags & portalocker.LockFlags.NON_BLOCKING)
True
EXCLUSIVE = 2

Request an exclusive lock. Only one process may hold EXCLUSIVE (or SHARED) on a given file at the same time; other processes attempting to lock it either block or fail, depending on whether NON_BLOCKING is also set.

NON_BLOCKING = 4

fail with AlreadyLocked immediately if it can’t be acquired right away, instead of blocking until it becomes available.

Type:

Don’t wait for the lock

SHARED = 1

Request a shared lock. Multiple processes may hold SHARED locks on the same file concurrently, but none may hold EXCLUSIVE while any SHARED lock is held. On Windows this requires the optional win32 extra (pip install "portalocker[win32]"); without it, acquiring a shared lock raises ImportError.

UNBLOCK = 8

Release a lock previously acquired on the same file. Used internally by portalocker.unlock; most callers use a context manager (Lock/RLock) instead of applying this flag directly.

__abs__()

abs(self)

__add__(value, /)

Return self+value.

__and__(other)

Return self&value.

__bool__()

True if self else False

__ceil__()

Ceiling of an Integral returns itself.

__contains__(other)

Returns True if self has at least the same flags set as other.

__dir__()

Returns public methods and other interesting attributes.

__divmod__(value, /)

Return divmod(self, value).

__eq__(value, /)

Return self==value.

__float__()

float(self)

__floor__()

Flooring an Integral returns itself.

__floordiv__(value, /)

Return self//value.

__format__(format_spec, /)

Convert to a string according to format_spec.

__ge__(value, /)

Return self>=value.

__getattribute__(name, /)

Return getattr(self, name).

classmethod __getitem__(name)

Return the member matching name.

__getnewargs__()
__gt__(value, /)

Return self>value.

__hash__()

Return hash(self).

__index__()

Return self converted to an integer, if self is suitable for use as an index into a list.

__init__(*args, **kwds)
__int__()

int(self)

__invert__()

~self

__iter__()

Returns flags in definition order.

__le__(value, /)

Return self<=value.

__len__()

Return the number of members (no aliases)

__lshift__(value, /)

Return self<<value.

__lt__(value, /)

Return self<value.

__members__ = mappingproxy({'EXCLUSIVE': <LockFlags.EXCLUSIVE: 2>, 'SHARED': <LockFlags.SHARED: 1>, 'NON_BLOCKING': <LockFlags.NON_BLOCKING: 4>, 'UNBLOCK': <LockFlags.UNBLOCK: 8>})
__mod__(value, /)

Return self%value.

__module__ = 'portalocker.constants'
__mul__(value, /)

Return self*value.

__name__ = 'LockFlags'
__ne__(value, /)

Return self!=value.

__neg__()

-self

__new__(value)
__or__(other)

Return self|value.

__pos__()

+self

__pow__(value, mod=None, /)

Return pow(self, value, mod).

__qualname__ = 'LockFlags'
__radd__(value, /)

Return value+self.

__rand__(other)

Return value&self.

__rdivmod__(value, /)

Return divmod(value, self).

__reduce_ex__(proto)

Helper for pickle.

__repr__()

Return repr(self).

__rfloordiv__(value, /)

Return value//self.

__rlshift__(value, /)

Return value<<self.

__rmod__(value, /)

Return value%self.

__rmul__(value, /)

Return value*self.

__ror__(other)

Return value|self.

__round__()

Rounding an Integral returns itself.

Rounding with an ndigits argument also returns an integer.

__rpow__(value, mod=None, /)

Return pow(value, self, mod).

__rrshift__(value, /)

Return value>>self.

__rshift__(value, /)

Return self>>value.

__rsub__(value, /)

Return value-self.

__rtruediv__(value, /)

Return value/self.

__rxor__(other)

Return value^self.

__sizeof__()

Returns size in memory, in bytes.

__str__()

Return repr(self).

__sub__(value, /)

Return self-value.

__truediv__(value, /)

Return self/value.

__trunc__()

Truncating an Integral returns itself.

__xor__(other)

Return self^value.

_all_bits_ = 15
_boundary_ = 'keep'
_flag_mask_ = 15
static _generate_next_value_(name, start, count, last_values)

Generate the next value when not given.

name: the name of the member start: the initial start value or None count: the number of existing members last_values: the last value assigned or None

_get_value(flag)
_inverted_ = None
classmethod _iter_member_(value)

Extract all members from the value in definition order.

classmethod _iter_member_by_def_(value)

Extract all members from the value in definition order.

classmethod _iter_member_by_value_(value)

Extract all members from the value in definition (i.e. increasing value) order.

classmethod _missing_(value)

Create a composite member containing all canonical members present in value.

If non-member values are present, result depends on _boundary_ setting.

_numeric_repr_(obj, /)

Return the canonical string representation of the object.

For many object types, including most builtins, eval(repr(obj)) == obj.

_singles_mask_ = 15
as_integer_ratio()

Return a pair of integers, whose ratio is equal to the original int.

The ratio is in lowest terms and has a positive denominator.

>>> (10).as_integer_ratio()
(10, 1)
>>> (-10).as_integer_ratio()
(-10, 1)
>>> (0).as_integer_ratio()
(0, 1)
bit_count()

Number of ones in the binary representation of the absolute value of self.

Also known as the population count.

>>> bin(13)
'0b1101'
>>> (13).bit_count()
3
bit_length()

Number of bits necessary to represent self in binary.

>>> bin(37)
'0b100101'
>>> (37).bit_length()
6
conjugate()

Returns self, the complex conjugate of any int.

denominator

the denominator of a rational number in lowest terms

classmethod from_bytes(bytes, byteorder='big', *, signed=False)

Return the integer represented by the given array of bytes.

bytes

Holds the array of bytes to convert. The argument must either support the buffer protocol or be an iterable object producing bytes. Bytes and bytearray are examples of built-in objects that support the buffer protocol.

byteorder

The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use sys.byteorder as the byte order value. Default is to use ‘big’.

signed

Indicates whether two’s complement is used to represent the integer.

imag

the imaginary part of a complex number

is_integer()

Returns True. Exists for duck type compatibility with float.is_integer.

numerator

the numerator of a rational number in lowest terms

real

the real part of a complex number

to_bytes(length=1, byteorder='big', *, signed=False)

Return an array of bytes representing an integer.

length

Length of bytes object to use. An OverflowError is raised if the integer is not representable with the given number of bytes. Default is length 1.

byteorder

The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use sys.byteorder as the byte order value. Default is to use ‘big’.

signed

Determines whether two’s complement is used to represent the integer. If signed is False and a negative integer is given, an OverflowError is raised.