Upload env/portalocker/__init__.py
Browse files- env/portalocker/__init__.py +149 -0
env/portalocker/__init__.py
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Cross-platform file locking, with optional Redis-backed distributed locks.
|
| 2 |
+
|
| 3 |
+
This is the public entry point of the ``portalocker`` package. Most of what
|
| 4 |
+
you need lives directly on this module:
|
| 5 |
+
|
| 6 |
+
- `Lock` and `RLock` open a file and hold an OS-level advisory lock on it
|
| 7 |
+
for the lifetime of the ``with`` block; `RLock` additionally allows the
|
| 8 |
+
same thread to re-enter the lock.
|
| 9 |
+
- `BoundedSemaphore` and `NamedBoundedSemaphore` cap the number of
|
| 10 |
+
processes that may hold a lock concurrently, using a directory of lock
|
| 11 |
+
files rather than a single one.
|
| 12 |
+
- `PidFileLock` writes the current process ID into the lock file, so a
|
| 13 |
+
stale lock left behind by a crashed process can be recognised.
|
| 14 |
+
- `TemporaryFileLock` is a short-lived variant of `Lock` intended for
|
| 15 |
+
small, throwaway critical sections.
|
| 16 |
+
- `RedisLock` is a distributed lock built on Redis pubsub, for
|
| 17 |
+
coordinating processes that do not share a filesystem. It requires the
|
| 18 |
+
optional ``redis`` dependency; when that package is not installed,
|
| 19 |
+
`RedisLock` is still importable - as a stub class whose constructor
|
| 20 |
+
raises ``ImportError`` - so that ``import portalocker`` never fails
|
| 21 |
+
and the missing dependency is only reported at use time. Install it
|
| 22 |
+
with ``pip install "portalocker[redis]"``.
|
| 23 |
+
- `lock` and `unlock` are the low-level, platform-specific primitives that
|
| 24 |
+
the classes above are built on; reach for them only if the context
|
| 25 |
+
managers do not fit your use case.
|
| 26 |
+
|
| 27 |
+
Example:
|
| 28 |
+
>>> import portalocker
|
| 29 |
+
>>> with portalocker.Lock('somefile', timeout=1) as fh:
|
| 30 |
+
... _ = fh.write('writing some stuff to my cache')
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
from . import __about__, constants, exceptions, portalocker
|
| 34 |
+
from .utils import (
|
| 35 |
+
BoundedSemaphore,
|
| 36 |
+
Lock,
|
| 37 |
+
NamedBoundedSemaphore,
|
| 38 |
+
PidFileLock,
|
| 39 |
+
RLock,
|
| 40 |
+
TemporaryFileLock,
|
| 41 |
+
open_atomic,
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
from .redis import RedisLock # pyright: ignore[reportAssignmentType]
|
| 46 |
+
except ImportError:
|
| 47 |
+
# `redis` is an optional dependency; keep the attribute importable so
|
| 48 |
+
# the missing dependency surfaces at use time, not import time. Before
|
| 49 |
+
# 4.2.0 the fallback was `None`, so constructing it failed with the
|
| 50 |
+
# baffling "TypeError: 'NoneType' object is not callable".
|
| 51 |
+
|
| 52 |
+
class RedisLock: # type: ignore[no-redef]
|
| 53 |
+
"""Stub for the optional Redis-backed lock.
|
| 54 |
+
|
| 55 |
+
Bound to ``portalocker.RedisLock`` only when the optional
|
| 56 |
+
``redis`` package is not installed. Constructing it names the
|
| 57 |
+
missing dependency and how to install it, instead of failing
|
| 58 |
+
with an inscrutable ``TypeError``.
|
| 59 |
+
"""
|
| 60 |
+
|
| 61 |
+
def __init__(self, *args: object, **kwargs: object) -> None:
|
| 62 |
+
"""Report the missing optional dependency.
|
| 63 |
+
|
| 64 |
+
Args:
|
| 65 |
+
args: Ignored, accepted for signature compatibility.
|
| 66 |
+
kwargs: Ignored, accepted for signature compatibility.
|
| 67 |
+
|
| 68 |
+
Raises:
|
| 69 |
+
ImportError: Always. ``RedisLock`` needs the optional
|
| 70 |
+
redis dependency, installable with
|
| 71 |
+
``pip install "portalocker[redis]"``.
|
| 72 |
+
"""
|
| 73 |
+
raise ImportError(
|
| 74 |
+
'portalocker.RedisLock requires the optional redis '
|
| 75 |
+
'dependency. Install it with: '
|
| 76 |
+
'pip install "portalocker[redis]"'
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
#: The package name on Pypi
|
| 81 |
+
__package_name__ = __about__.__package_name__
|
| 82 |
+
#: Current author and maintainer, view the git history for the previous ones
|
| 83 |
+
__author__ = __about__.__author__
|
| 84 |
+
#: Current author's email address
|
| 85 |
+
__email__ = __about__.__email__
|
| 86 |
+
#: Version number
|
| 87 |
+
__version__ = __about__.__version__
|
| 88 |
+
#: Package description for Pypi
|
| 89 |
+
__description__ = __about__.__description__
|
| 90 |
+
#: Package homepage
|
| 91 |
+
__url__ = __about__.__url__
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
#: Exception thrown when the file is already locked by someone else
|
| 95 |
+
AlreadyLocked = exceptions.AlreadyLocked
|
| 96 |
+
#: Exception thrown if an error occurred during locking
|
| 97 |
+
LockException = exceptions.LockException
|
| 98 |
+
#: Exception thrown when a held `RedisLock` was revoked from outside
|
| 99 |
+
LockLostError = exceptions.LockLostError
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
#: Lock a file. Note that this is an advisory lock on Linux/Unix systems
|
| 103 |
+
lock = portalocker.lock
|
| 104 |
+
#: Unlock a file
|
| 105 |
+
unlock = portalocker.unlock
|
| 106 |
+
|
| 107 |
+
#: Place an exclusive lock.
|
| 108 |
+
#: Only one process may hold an exclusive lock for a given file at a given
|
| 109 |
+
#: time.
|
| 110 |
+
LOCK_EX: constants.LockFlags = constants.LockFlags.EXCLUSIVE
|
| 111 |
+
|
| 112 |
+
#: Place a shared lock.
|
| 113 |
+
#: More than one process may hold a shared lock for a given file at a given
|
| 114 |
+
#: time.
|
| 115 |
+
LOCK_SH: constants.LockFlags = constants.LockFlags.SHARED
|
| 116 |
+
|
| 117 |
+
#: Acquire the lock in a non-blocking fashion.
|
| 118 |
+
LOCK_NB: constants.LockFlags = constants.LockFlags.NON_BLOCKING
|
| 119 |
+
|
| 120 |
+
#: Flag used internally by `unlock` to release a held lock. Passing it
|
| 121 |
+
#: to `lock` raises ``RuntimeError`` since 4.2.0 - call `unlock` instead.
|
| 122 |
+
LOCK_UN: constants.LockFlags = constants.LockFlags.UNBLOCK
|
| 123 |
+
|
| 124 |
+
#: Locking flags enum
|
| 125 |
+
LockFlags = constants.LockFlags
|
| 126 |
+
|
| 127 |
+
#: Locking utility class to automatically handle opening with timeouts and
|
| 128 |
+
#: context wrappers
|
| 129 |
+
|
| 130 |
+
__all__ = [
|
| 131 |
+
'LOCK_EX',
|
| 132 |
+
'LOCK_NB',
|
| 133 |
+
'LOCK_SH',
|
| 134 |
+
'LOCK_UN',
|
| 135 |
+
'AlreadyLocked',
|
| 136 |
+
'BoundedSemaphore',
|
| 137 |
+
'Lock',
|
| 138 |
+
'LockException',
|
| 139 |
+
'LockFlags',
|
| 140 |
+
'LockLostError',
|
| 141 |
+
'NamedBoundedSemaphore',
|
| 142 |
+
'PidFileLock',
|
| 143 |
+
'RLock',
|
| 144 |
+
'RedisLock',
|
| 145 |
+
'TemporaryFileLock',
|
| 146 |
+
'lock',
|
| 147 |
+
'open_atomic',
|
| 148 |
+
'unlock',
|
| 149 |
+
]
|