Metadata-Version: 2.1
Name: simple_file_lock
Version: 2023.2
Summary: Context manager for simple file locking.
Project-URL: Source, https://gitlab.inria.fr/jrye/simple-file-lock
Project-URL: Documentation, https://jrye.gitlabpages.inria.fr/simple-file-lock/
Author-email: Jan-Michael Rye <jan-michael.rye@inria.fr>
License: MIT License
        
        Copyright (c) 2023, Inria
        
        Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
License-File: LICENSE.txt
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.7
Requires-Dist: psutil
Description-Content-Type: text/markdown

---
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](https://pypi.org/project/psutil/).

# Usage

~~~python
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](https://jrye.gitlabpages.inria.fr/simple-file-lock/).

# Unit Tests

Some basic unit tests have been implemented and can be run in a virtual environment with [test.sh](scripts/test.sh).
