lockfile Package

Cross-platform (posix/windows) API for flock-style file locking. This implementation is based on Roundup Issue-Tracking System ctypes port, which in turn, is based on an Active State’s Python Recipe portalocker.

class configviper.lockfile.PortaLocker(filename, mode, encoding='utf-8', ignore_encoding=False, sync_before_unlock=True)

Base class for actual file lock implementations.

lock()

Open file and attempt to lock it. Returns True on success.

unlock()

Attempt to unlock and close the file.

configviper.lockfile.lockfile(filename, mode, encoding='utf-8', ignore_encoding=False)

Attempt to instantiate and return an appropriate file lock implementation which depends on the underlying operating system. The best way to use the returned implementation is in a managed context with with statement or use the methods lock() and unlock() directly:

with lockfile('/home/john/eggs.txt', 'wb') as locker:
    # here, eggs.txt was opened and the file-like object can be
    # accessed through locker.file_object attribute, e.g.:
    locker.file_object.write(data)

When the context ends, the file is unlocked and closed, in that order.

Parameters:
  • filename – String. The full path name to the file that should be locked. The file will be opened using the mode parameter and then locked.
  • mode – String. The mode as in codecs.open or open.
  • encoding – String. The encoding which is to be used for the file, as in codecs.open encoding parameter. This parameter will be ignored if ignore_encoding is True.
  • ignore_encoding – Boolean. If False, which is the default, the file will be opened with codecs.open. Otherwise, the file will be opened with the Python’s open built-in function.

posixlocker Module

class configviper.lockfile.posixlocker.PosixPortaLocker(filename, mode, encoding='utf-8', ignore_encoding=False)

File lock implementation based on fcntl.lockf function for exclusive file locking (fcntl.LOCK_EX).

ctypeslocker Module

class configviper.lockfile.posixlocker.CTypesPortaLocker

File lock implementation targeting Windows TM based on ctypes Python package, using underlying LockFileEx and UnlockFileEx functions.

Table Of Contents

Previous topic

configviper Package

This Page