Context manager for simple file locking.
Project description
title: README author: Jan-Michael Rye
Synopsis
A context manager for rudimentary file locking. Lock files are generated next to the target path using the current hostname, process ID (PID) and a random hexadecimal token. Access to the file is then granted in order of ascending modification times of the associated lock files. If two or more lock files for the same path have the same modification time, access is granted in the sort order of the lock file paths. Each context waits for its turn to access the file.
The associated lock file is deleted on exit from the context. The context also automatically deletes dead lock files for non-existent processes on the same host. Dead locks are detected by checking if the associated process has exited with psutil.
Links
Usage
from simple_file_lock import FileLock
# ...
# Gain exclusive access to a path by creating a context with FileLock:
with FileLock(path):
# Do whatever you want with the file here. When the context is exited, the
# lock is released.
FileLock
also accepts an interval
parameter to configure the polling interval when waiting for existing locks to be released.
The Sphinx-generated API documentation is available here.
Unit Tests
Some basic unit tests have been implemented and can be run in a virtual environment with test.sh.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file simple_file_lock-2024.1.tar.gz
.
File metadata
- Download URL: simple_file_lock-2024.1.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 35441b77b71a6ab41e94a8814d872e53b686f6837463750e71dab1f3e4f6faa6 |
|
MD5 | 4cc75be0781accaf55e2df39da6b19d3 |
|
BLAKE2b-256 | 43ac961569abf6af147f5e476ea96ec5870f92ed0e1231dda640db8784039a4f |
File details
Details for the file simple_file_lock-2024.1-py3-none-any.whl
.
File metadata
- Download URL: simple_file_lock-2024.1-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f5804e64d3e9833dd0a3ff7d1939cb7e7233de740ef6eb37b165ff96fd9df93d |
|
MD5 | 8f7105ee61a176ca8ffbad630154ce46 |
|
BLAKE2b-256 | 06260c759bd75b651431b498514c7380def36c4a25f4d123f0f39e1b70558669 |