https://github.com/rossmacarthur/locks
POSIX file system locking using flock
https://github.com/rossmacarthur/locks
flock posix python python3
Last synced: 5 months ago
JSON representation
POSIX file system locking using flock
- Host: GitHub
- URL: https://github.com/rossmacarthur/locks
- Owner: rossmacarthur
- License: apache-2.0
- Archived: true
- Created: 2019-11-13T07:57:07.000Z (over 6 years ago)
- Default Branch: trunk
- Last Pushed: 2021-04-20T20:13:15.000Z (about 5 years ago)
- Last Synced: 2025-09-23T13:47:46.210Z (9 months ago)
- Topics: flock, posix, python, python3
- Language: Python
- Homepage:
- Size: 22.5 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# locks
*POSIX file system locking using [flock](https://linux.die.net/man/2/flock).*
[](https://pypi.org/project/locks/)
[](https://github.com/rossmacarthur/locks/actionsquery=workflow%3Abuild)
[](https://codecov.io/gh/rossmacarthur/locks)
[](https://github.com/psf/black)
## Getting started
Install this package with
```sh
pip install locks
```
## Usage
The simplest usage is to block indefinitely until the lock is acquired
```python
from locks import Mutex
with Mutex('/tmp/file.lock'):
# do exclusive stuff here
```
Alternatively, block until a timeout is reached
```python
from locks import Mutex
try:
with Mutex('/tmp/file.lock', timeout=0.5):
# do exclusive stuff here
except BlockingIOError:
# handle the failure to acquire the lock
```
Finally, a common paradigm is to attempt to acquire the lock without blocking,
do something, and then block indefinitely. Here `callback` will be called once
if we cannot immediately acquire the lock, and then we will block indefinitely.
```python
def callback():
print("Blocking: waiting for file lock on '/tmp/file.lock'")
with Mutex('/tmp/file.lock', callback=callback):
# do exclusive stuff here
```
If both `callback` and `timeout` are used then we will attempt to
acquire the lock until the `timeout` is reached, and then we will block
indefinitely.
## License
Licensed under either of
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or
http://www.apache.org/licenses/LICENSE-2.0)
- MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.