Skip to main content

POSIX file system locking using flock

Project description

locks

PyPI version Build status

POSIX file system locking using flock.

Getting started

Install this package with

pip install locks

Usage

The simplest usage is to block indefinitely until the lock is acquired

from locks import Mutex

with Mutex('/tmp/file.lock'):
    # do exclusive stuff here

Alternatively, block until a timeout is reached

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.

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

This project is licensed under the MIT License. See the LICENSE file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for locks, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size locks-0.1.1-py2.py3-none-any.whl (3.9 kB) File type Wheel Python version py2.py3 Upload date Hashes View hashes
Filename, size locks-0.1.1.tar.gz (3.4 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page