caterpillar package

caterpillar.locking module

Locking on a file implemented via Unix PID files. Based on https://github.com/smontanaro/pylockfile/blob/master/lockfile/pidlockfile.py

exception caterpillar.locking.AlreadyLocked

Bases: caterpillar.locking.LockError

Some other thread/process is locking the file.

exception caterpillar.locking.LockError

Bases: exceptions.Exception

Base class for error arising from attempts to acquire the lock.

exception caterpillar.locking.LockFailed

Bases: caterpillar.locking.LockError

Lock file creation failed for some other reason.

exception caterpillar.locking.LockTimeout

Bases: caterpillar.locking.LockError

Raised when lock creation fails within a user-defined period of time.

exception caterpillar.locking.NotLocked

Bases: caterpillar.locking.UnlockError

Raised when an attempt is made to unlock an unlocked file.

exception caterpillar.locking.NotMyLock

Bases: caterpillar.locking.UnlockError

Raised when an attempt is made to unlock a file someone else locked.

class caterpillar.locking.PIDLockFile(path)

Bases: object

Locking implemented as a Unix PID file.

The lock file is a normal file named by the attribute path. A lock’s PID file contains a single line of text, containing the process ID (PID) of the process that acquired the lock.

WARNING This is NOT a reentrant lock!

acquire(timeout=None)

Acquire the lock.

Creates the PID file for this lock, or raises an error if the lock could not be acquired.

If timeout is omitted (or None), wait forever trying to lock the file.

If timeout > 0, try to acquire the lock for that many seconds. If the lock period expires and the file is still locked, raise LockTimeout.

If timeout <= 0, raise AlreadyLocked immediately if the file is already locked.

i_am_locking()

Test if the lock is held by the current process.

Returns True if the current process ID matches the number stored in the PID file.

is_locked()

Test if the lock is currently held.

The lock is held if the PID file for this lock exists.

read_pid()

Get the PID from the lock file.

release()

Release the lock.

Removes the PID file to release the lock, or raises an error if the current process does not hold the lock.

exception caterpillar.locking.UnlockError

Bases: exceptions.Exception

Base class for errors arising from attempts to release the lock.