Download version
2.0 (changes)
This software is in the public domain (there is no license).
Class LockError
Class LockFile
Examples
Lock file is a traditional means of synchronization among processes. In this module it is implemented as an empty regular file exclusively locked using fcntl.lockf. When it is to be released it is removed by default. However, if all cooperating processes turn off the removal, they get a guaranteed order of acquisitions and better scalability.
Parent class: Exception
Raised when a lock file acquisition is unsuccessful because the lock file is held by another process.
Parent class: object
This class represents an acquired lock file. After its releasing most methods lose their sense and raise a ValueError.
Raises LockError if the wait argument is False and the lock file is held by another process. Raises OSError or IOError if any other error occurs. In particular, raises IOError with the errno attribute set to errno.EINTR, if waiting for the acquisition is interrupted by a signal.
Raises ValueError if the lock file is released.
Raises exceptions raised by release.
Raises ValueError if the lock file is released.
Raises ValueError if the lock file is released.
Raises ValueError if the lock file is already released. Raises OSError if any other error occurs.
import sys from lock_file import LockFile, LockError try: lock_f = LockFile('/var/run/app.lock') except LockError: sys.exit('The script is already running') try: do_something_useful() finally: lock_f.release()
from lock_file import LockFile lock_f = LockFile('/var/run/app.lock', wait = True) try: do_something_useful() finally: lock_f.release()
from __future__ import with_statement from lock_file import LockFile with LockFile('/var/run/app.lock', wait = True): do_something_useful()