| 1 |
Metadata-Version: 2.4 |
| 2 |
Name: portalocker |
| 3 |
Version: 3.2.0 |
| 4 |
Summary: Wraps the portalocker recipe for easy usage |
| 5 |
Author-email: Rick van Hattem <[email protected]> |
| 6 |
License-Expression: BSD-3-Clause |
| 7 |
Project-URL: bugs, https://github.com/wolph/portalocker/issues |
| 8 |
Project-URL: documentation, https://portalocker.readthedocs.io/en/latest/ |
| 9 |
Project-URL: repository, https://github.com/wolph/portalocker/ |
| 10 |
Keywords: locking,locks,with,statement,windows,linux,unix |
| 11 |
Platform: any |
| 12 |
Classifier: Development Status :: 5 - Production/Stable |
| 13 |
Classifier: Development Status :: 6 - Mature |
| 14 |
Classifier: Intended Audience :: Developers |
| 15 |
Classifier: Natural Language :: English |
| 16 |
Classifier: Operating System :: MacOS :: MacOS X |
| 17 |
Classifier: Operating System :: MacOS |
| 18 |
Classifier: Operating System :: Microsoft :: MS-DOS |
| 19 |
Classifier: Operating System :: Microsoft :: Windows |
| 20 |
Classifier: Operating System :: Microsoft |
| 21 |
Classifier: Operating System :: POSIX :: BSD :: FreeBSD |
| 22 |
Classifier: Operating System :: POSIX :: BSD |
| 23 |
Classifier: Operating System :: POSIX :: Linux |
| 24 |
Classifier: Operating System :: POSIX :: SunOS/Solaris |
| 25 |
Classifier: Operating System :: POSIX |
| 26 |
Classifier: Operating System :: Unix |
| 27 |
Classifier: Programming Language :: Python :: 3 :: Only |
| 28 |
Classifier: Programming Language :: Python :: 3 |
| 29 |
Classifier: Programming Language :: Python :: 3.9 |
| 30 |
Classifier: Programming Language :: Python :: 3.10 |
| 31 |
Classifier: Programming Language :: Python :: 3.11 |
| 32 |
Classifier: Programming Language :: Python :: 3.12 |
| 33 |
Classifier: Programming Language :: Python :: 3.13 |
| 34 |
Classifier: Programming Language :: Python :: Implementation :: CPython |
| 35 |
Classifier: Programming Language :: Python :: Implementation :: IronPython |
| 36 |
Classifier: Programming Language :: Python :: Implementation :: PyPy |
| 37 |
Classifier: Programming Language :: Python :: Implementation |
| 38 |
Classifier: Programming Language :: Python |
| 39 |
Classifier: Topic :: Education :: Testing |
| 40 |
Classifier: Topic :: Office/Business |
| 41 |
Classifier: Topic :: Other/Nonlisted Topic |
| 42 |
Classifier: Topic :: Software Development :: Libraries :: Python Modules |
| 43 |
Classifier: Topic :: Software Development :: Libraries |
| 44 |
Classifier: Topic :: System :: Monitoring |
| 45 |
Classifier: Typing :: Typed |
| 46 |
Requires-Python: >=3.9 |
| 47 |
Description-Content-Type: text/x-rst |
| 48 |
License-File: LICENSE |
| 49 |
Requires-Dist: pywin32>=226; platform_system == "Windows" |
| 50 |
Provides-Extra: docs |
| 51 |
Requires-Dist: portalocker[tests]; extra == "docs" |
| 52 |
Provides-Extra: tests |
| 53 |
Requires-Dist: coverage-conditional-plugin>=0.9.0; extra == "tests" |
| 54 |
Requires-Dist: portalocker[redis]; extra == "tests" |
| 55 |
Requires-Dist: pytest-cov>=2.8.1; extra == "tests" |
| 56 |
Requires-Dist: pytest-mypy>=0.8.0; extra == "tests" |
| 57 |
Requires-Dist: pytest-rerunfailures>=15.0; extra == "tests" |
| 58 |
Requires-Dist: pytest-timeout>=2.1.0; extra == "tests" |
| 59 |
Requires-Dist: pytest>=5.4.1; extra == "tests" |
| 60 |
Requires-Dist: sphinx>=6.0.0; extra == "tests" |
| 61 |
Requires-Dist: types-pywin32>=310.0.0.20250429; extra == "tests" |
| 62 |
Requires-Dist: types-redis; extra == "tests" |
| 63 |
Provides-Extra: redis |
| 64 |
Requires-Dist: redis; extra == "redis" |
| 65 |
Dynamic: license-file |
| 66 |
|
| 67 |
############################################ |
| 68 |
portalocker - Cross-platform locking library |
| 69 |
############################################ |
| 70 |
|
| 71 |
.. image:: https://github.com/WoLpH/portalocker/actions/workflows/python-package.yml/badge.svg?branch=master |
| 72 |
:alt: Linux Test Status |
| 73 |
:target: https://github.com/WoLpH/portalocker/actions/ |
| 74 |
|
| 75 |
.. image:: https://ci.appveyor.com/api/projects/status/mgqry98hgpy4prhh?svg=true |
| 76 |
:alt: Windows Tests Status |
| 77 |
:target: https://ci.appveyor.com/project/WoLpH/portalocker |
| 78 |
|
| 79 |
.. image:: https://coveralls.io/repos/WoLpH/portalocker/badge.svg?branch=master |
| 80 |
:alt: Coverage Status |
| 81 |
:target: https://coveralls.io/r/WoLpH/portalocker?branch=master |
| 82 |
|
| 83 |
Overview |
| 84 |
-------- |
| 85 |
|
| 86 |
Portalocker is a library to provide an easy API to file locking. |
| 87 |
|
| 88 |
An important detail to note is that on Linux and Unix systems the locks are |
| 89 |
advisory by default. By specifying the `-o mand` option to the mount command it |
| 90 |
is possible to enable mandatory file locking on Linux. This is generally not |
| 91 |
recommended however. For more information about the subject: |
| 92 |
|
| 93 |
- https://en.wikipedia.org/wiki/File_locking |
| 94 |
- http://stackoverflow.com/questions/39292051/portalocker-does-not-seem-to-lock |
| 95 |
- https://stackoverflow.com/questions/12062466/mandatory-file-lock-on-linux |
| 96 |
|
| 97 |
The module is currently maintained by Rick van Hattem <[email protected]>. |
| 98 |
The project resides at https://github.com/WoLpH/portalocker . Bugs and feature |
| 99 |
requests can be submitted there. Patches are also very welcome. |
| 100 |
|
| 101 |
Security contact information |
| 102 |
------------------------------------------------------------------------------ |
| 103 |
|
| 104 |
To report a security vulnerability, please use the |
| 105 |
`Tidelift security contact <https://tidelift.com/security>`_. |
| 106 |
Tidelift will coordinate the fix and disclosure. |
| 107 |
|
| 108 |
Redis Locks |
| 109 |
----------- |
| 110 |
|
| 111 |
This library now features a lock based on Redis which allows for locks across |
| 112 |
multiple threads, processes and even distributed locks across multiple |
| 113 |
computers. |
| 114 |
|
| 115 |
It is an extremely reliable Redis lock that is based on pubsub. |
| 116 |
|
| 117 |
As opposed to most Redis locking systems based on key/value pairs, |
| 118 |
this locking method is based on the pubsub system. The big advantage is |
| 119 |
that if the connection gets killed due to network issues, crashing |
| 120 |
processes or otherwise, it will still immediately unlock instead of |
| 121 |
waiting for a lock timeout. |
| 122 |
|
| 123 |
First make sure you have everything installed correctly: |
| 124 |
|
| 125 |
:: |
| 126 |
|
| 127 |
pip install "portalocker[redis]" |
| 128 |
|
| 129 |
Usage is really easy: |
| 130 |
|
| 131 |
:: |
| 132 |
|
| 133 |
import portalocker |
| 134 |
|
| 135 |
lock = portalocker.RedisLock('some_lock_channel_name') |
| 136 |
|
| 137 |
with lock: |
| 138 |
print('do something here') |
| 139 |
|
| 140 |
The API is essentially identical to the other ``Lock`` classes so in addition |
| 141 |
to the ``with`` statement you can also use ``lock.acquire(...)``. |
| 142 |
|
| 143 |
Python 2 |
| 144 |
-------- |
| 145 |
|
| 146 |
Python 2 was supported in versions before Portalocker 2.0. If you are still |
| 147 |
using |
| 148 |
Python 2, |
| 149 |
you can run this to install: |
| 150 |
|
| 151 |
:: |
| 152 |
|
| 153 |
pip install "portalocker<2" |
| 154 |
|
| 155 |
Tips |
| 156 |
---- |
| 157 |
|
| 158 |
On some networked filesystems it might be needed to force a `os.fsync()` before |
| 159 |
closing the file so it's actually written before another client reads the file. |
| 160 |
Effectively this comes down to: |
| 161 |
|
| 162 |
:: |
| 163 |
|
| 164 |
with portalocker.Lock('some_file', 'rb+', timeout=60) as fh: |
| 165 |
# do what you need to do |
| 166 |
... |
| 167 |
|
| 168 |
# flush and sync to filesystem |
| 169 |
fh.flush() |
| 170 |
os.fsync(fh.fileno()) |
| 171 |
|
| 172 |
Links |
| 173 |
----- |
| 174 |
|
| 175 |
* Documentation |
| 176 |
- http://portalocker.readthedocs.org/en/latest/ |
| 177 |
* Source |
| 178 |
- https://github.com/WoLpH/portalocker |
| 179 |
* Bug reports |
| 180 |
- https://github.com/WoLpH/portalocker/issues |
| 181 |
* Package homepage |
| 182 |
- https://pypi.python.org/pypi/portalocker |
| 183 |
* My blog |
| 184 |
- http://w.wol.ph/ |
| 185 |
|
| 186 |
Examples |
| 187 |
-------- |
| 188 |
|
| 189 |
To make sure your cache generation scripts don't race, use the `Lock` class: |
| 190 |
|
| 191 |
>>> import portalocker |
| 192 |
>>> with portalocker.Lock('somefile', timeout=1) as fh: |
| 193 |
... print('writing some stuff to my cache...', file=fh) |
| 194 |
|
| 195 |
To customize the opening and locking a manual approach is also possible: |
| 196 |
|
| 197 |
>>> import portalocker |
| 198 |
>>> file = open('somefile', 'r+') |
| 199 |
>>> portalocker.lock(file, portalocker.LockFlags.EXCLUSIVE) |
| 200 |
>>> file.seek(12) |
| 201 |
>>> file.write('foo') |
| 202 |
>>> file.close() |
| 203 |
|
| 204 |
Explicitly unlocking is not needed in most cases but omitting it has been known |
| 205 |
to cause issues: |
| 206 |
https://github.com/AzureAD/microsoft-authentication-extensions-for-python/issues/42#issuecomment-601108266 |
| 207 |
|
| 208 |
If needed, it can be done through: |
| 209 |
|
| 210 |
>>> portalocker.unlock(file) |
| 211 |
|
| 212 |
Do note that your data might still be in a buffer so it is possible that your |
| 213 |
data is not available until you `flush()` or `close()`. |
| 214 |
|
| 215 |
To create a cross platform bounded semaphore across multiple processes you can |
| 216 |
use the `BoundedSemaphore` class which functions somewhat similar to |
| 217 |
`threading.BoundedSemaphore`: |
| 218 |
|
| 219 |
>>> import portalocker |
| 220 |
>>> n = 2 |
| 221 |
>>> timeout = 0.1 |
| 222 |
|
| 223 |
>>> semaphore_a = portalocker.BoundedSemaphore(n, timeout=timeout) |
| 224 |
>>> semaphore_b = portalocker.BoundedSemaphore(n, timeout=timeout) |
| 225 |
>>> semaphore_c = portalocker.BoundedSemaphore(n, timeout=timeout) |
| 226 |
|
| 227 |
>>> semaphore_a.acquire() |
| 228 |
<portalocker.utils.Lock object at ...> |
| 229 |
>>> semaphore_b.acquire() |
| 230 |
<portalocker.utils.Lock object at ...> |
| 231 |
>>> semaphore_c.acquire() |
| 232 |
Traceback (most recent call last): |
| 233 |
... |
| 234 |
portalocker.exceptions.AlreadyLocked |
| 235 |
|
| 236 |
|
| 237 |
More examples can be found in the |
| 238 |
`tests <http://portalocker.readthedocs.io/en/latest/_modules/tests/tests.html>`_. |
| 239 |
|
| 240 |
|
| 241 |
Versioning |
| 242 |
---------- |
| 243 |
|
| 244 |
This library follows `Semantic Versioning <http://semver.org/>`_. |
| 245 |
|
| 246 |
|
| 247 |
Changelog |
| 248 |
--------- |
| 249 |
|
| 250 |
Every release has a ``git tag`` with a commit message for the tag |
| 251 |
explaining what was added and/or changed. The list of tags/releases |
| 252 |
including the commit messages can be found here: |
| 253 |
https://github.com/WoLpH/portalocker/releases |
| 254 |
|
| 255 |
License |
| 256 |
------- |
| 257 |
|
| 258 |
See the `LICENSE <https://github.com/WoLpH/portalocker/blob/develop/LICENSE>`_ file. |
| 259 |
|