Process lock

Classes

fasteners.process_lock.InterProcessLock

alias of fasteners.process_lock._FcntlLock

class fasteners.process_lock._InterProcessLock(path, sleep_func=<built-in function sleep>, logger=None)

An interprocess locking implementation.

This is a lock implementation which allows multiple locks, working around issues like http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=632857 and does not require any cleanup. Since the lock is always held on a file descriptor rather than outside of the process, the lock gets dropped automatically if the process crashes, even if __exit__ is not executed.

There are no guarantees regarding usage by multiple threads in a single process here. This lock works only between processes.

Note these locks are released when the descriptor is closed, so it’s not safe to close the file descriptor while another thread holds the lock. Just opening and closing the lock file can break synchronization, so lock files must be accessed only using this abstraction.

Warning

It is quite useful to read before using (to understand the risks involved): http://0pointer.de/blog/projects/locking.html

acquire(blocking=True, delay=0.01, max_delay=0.1, timeout=None)

Attempt to acquire the given lock.

Parameters:
  • blocking (bool) – whether to wait forever to try to acquire the lock
  • delay (int/float) – when blocking this is the delay time in seconds that will be added after each failed acquisition
  • max_delay (int/float) – the maximum delay to have (this limits the accumulated delay(s) added after each failed acquisition)
  • timeout (int/float) – an optional timeout (limits how long blocking will occur for)
Returns:

whether or not the acquisition succeeded

Return type:

bool

exists()

Checks if the path that this lock exists at actually exists.

release()

Release the previously acquired lock.

Decorators

fasteners.process_lock.interprocess_locked(path)

Acquires & releases a interprocess lock around call into decorated function.