labm8.lockfile

Lock file mechanism.

exception labm8.lockfile.Error
class labm8.lockfile.LockFile(path)

A lock file.

path

str – Path of lock file.

acquire(replace_stale=False, force=False)

Acquire the lock.

A lock can be claimed if any of these conditions are true:
  1. The lock is unheld by anyone.
  2. The lock is held but the ‘force’ argument is set.
  3. The lock is held by the current process.
Parameters:
  • replace_stale (bool, optional) – stale processes. A stale process is one which currently owns the parent lock, but no process with that PID is alive.
  • force (bool, optional) – If true, ignore any existing lock. If false, fail if lock already claimed.
Returns:

self.

Return type:

LockFile

Raises:

UnableToAcquireLockError – If the lock is already claimed (not raised if force option is used).

date

The date that the lock was acquired. Value is None if lock is unclaimed.

islocked

Whether the directory is locked.

owned_by_self

Whether the directory is locked by the current process.

pid

The process ID of the lock. Value is None if lock is not claimed.

static read(path)

Read the contents of a LockFile.

Parameters:path (str) – Path to lockfile.
Returns:
The integer PID of the lock owner, and the
date the lock was required. If the lock is not claimed, both values are None.
Return type:Tuple(int, datetime)
release(force=False)

Release lock.

To release a lock, we must already own the lock.

Parameters:force (bool, optional) – If true, ignore any existing lock owner.
Raises:UnableToReleaseLockError – If the lock is claimed by another process (not raised if force option is used).
static write(path, pid, timestamp)

Write the contents of a LockFile.

Parameters:
  • path (str) – Path to lockfile.
  • pid (int) – The integer process ID.
  • timestamp (datetime) – The time the lock was aquired.
exception labm8.lockfile.UnableToAcquireLockError(lock)

thrown if cannot acquire lock

exception labm8.lockfile.UnableToReleaseLockError(lock)

thrown if cannot release lock