Skip to main content

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.

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

simple_file_lock-2023.5.tar.gz (7.5 kB view hashes)

Uploaded Source

Built Distribution

simple_file_lock-2023.5-py3-none-any.whl (5.2 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page