Skip to content

Inter process locks

Fasteners inter-process locks are cross-platform and are released automatically if the process crashes. They are based on the platform specific locking mechanisms:

  • fcntl for posix (Linux and OSX)
  • LockFileEx (via pywin32) and _locking (via msvcrt) for Windows

Difference from multiprocessing.Lock

Python standard library multiprocessing.Lock functions when the processes are launched by a single main process, who is responsible for managing the synchronization. fasteners locks use the operating system mechanisms for synchronization management, and hence work between processes that were launched independently.


fasteners locks support timeouts, that can be used as follows:

import fasteners

lock = fasteners.InterProcessLock('path/to/lock.file')

... # exclusive access

Equivalently for readers writer lock:

import fasteners

lock = fasteners.InterProcessReaderWriterLock('path/to/lock.file')

... # exclusive access

... # exclusive access


For extra sugar, a function that always needs exclusive / read / write access can be decorated using one of the provided decorators. Note that they do not expose the timeout parameter, and always block until the lock is acquired.

import fasteners

def read_file():

def write_file():

def do_something_exclusive():

(Lack of) Features

The intersection of fcntl and LockFileEx features is quite small, hence you should always assume that:

  • Locks are advisory. They do not prevent the modification of the locked file by other processes.

  • Locks can be unintentionally released by simply opening and closing the file descriptor, so lock files must be accessed only using provided abstractions.

  • Locks are not reentrant. An attempt to acquire a lock multiple times can result in a deadlock or a crash upon a release of the lock.

  • Reader writer locks are not upgradeable. An attempt to get a reader's lock while holding a writer's lock (or vice versa) can result in a deadlock or a crash upon a release of the lock.

  • There are no guarantees regarding usage by multiple threads in a single process. The locks work only between processes.


To learn more about the complications of locking on different platforms we recommend the following resources: