API Reference¶
This section documents all public classes, exceptions, and attributes. For usage examples and task-oriented guidance, see Tutorials and How-to guides.
FileLock and AsyncFileLock are platform aliases: at import time they resolve to
UnixFileLock or WindowsFileLock (and their async peers), or to the soft backends on
a build without fcntl. The shared acquire / release / timeout interface they expose lives on
BaseFileLock and BaseAsyncFileLock below.
A platform independent file lock that supports the with-statement.
- filelock.__version__¶
version of the project as a string
- class filelock.AcquireReturnProxy(lock)[source]¶
Bases:
objectA context-aware object that will release the lock file when exiting.
- class filelock.AsyncAcquireReadWriteReturnProxy(lock)[source]¶
Bases:
objectContext-aware object that releases the async read/write lock on exit.
- class filelock.AsyncAcquireReturnProxy(lock)[source]¶
Bases:
objectA context-aware object that will release the lock file when exiting.
- class filelock.AsyncAcquireSoftReadWriteReturnProxy(lock)[source]¶
Bases:
objectAsync context-aware object that releases an
AsyncSoftReadWriteLockon exit.
- filelock.AsyncFileLock¶
alias of
AsyncUnixFileLock
- class filelock.AsyncReadWriteLock(lock_file, timeout=-1, *, blocking=True, is_singleton=True, loop=None, executor=None)[source]¶
Bases:
objectAsync wrapper around
ReadWriteLockfor use inasyncioapplications.This wrapper dispatches every blocking SQLite operation to a thread pool via
loop.run_in_executor()because Python’ssqlite3module has no async API. It delegates reentrancy, upgrade/downgrade rules, and singleton behavior to the underlyingReadWriteLock.- Parameters:
lock_file (
str|PathLike[str]) – path to the SQLite database file used as the locktimeout (
float) – maximum wait time in seconds;-1means block indefinitelyblocking (
bool) – ifFalse, raiseTimeoutimmediately when the lock is unavailableis_singleton (
bool) – ifTrue, reuse existingReadWriteLockinstances for the same resolved pathloop (
AbstractEventLoop|None) – event loop forrun_in_executor;Noneuses the running loopexecutor (
Executor|None) – executor forrun_in_executor. WhenNonethis lock creates and owns a dedicated single-thread executor so every operation runs on the same thread (SQLite affinity requires this) and shuts it down inclose(). This lock uses a caller-supplied executor as-is and never shuts it down, so after passing no executor callclose()to release the owned one.
Added in version 3.21.0.
- property lock_file¶
The path to the lock file.
- property timeout¶
The default timeout.
- property blocking¶
Whether blocking is enabled by default.
- property loop¶
The event loop (or
Nonefor the running loop).
- property executor¶
The executor used for
run_in_executor(a dedicated single-thread one if none was supplied).
- read_lock(timeout=None, *, blocking=None)[source]¶
Async context manager that acquires and releases a shared read lock.
Falls back to instance defaults for timeout and blocking when
None.
- write_lock(timeout=None, *, blocking=None)[source]¶
Async context manager that acquires and releases an exclusive write lock.
Falls back to instance defaults for timeout and blocking when
None.
- async acquire_read(timeout=-1, *, blocking=True)[source]¶
Acquire a shared read lock.
See
ReadWriteLock.acquire_read()for full semantics.- Parameters:
- Return type:
- Returns:
a proxy that can be used as an async context manager to release the lock
- Raises:
RuntimeError – if a write lock is already held on this instance
Timeout – if the lock cannot be acquired within timeout seconds
- async acquire_write(timeout=-1, *, blocking=True)[source]¶
Acquire an exclusive write lock.
See
ReadWriteLock.acquire_write()for full semantics.- Parameters:
- Return type:
- Returns:
a proxy that can be used as an async context manager to release the lock
- Raises:
RuntimeError – if a read lock is already held, or a write lock is held by a different thread
Timeout – if the lock cannot be acquired within timeout seconds
- async release(*, force=False)[source]¶
Release one level of the current lock.
See
ReadWriteLock.release()for full semantics.- Parameters:
force (
bool) – ifTrue, release the lock completely regardless of the current lock level- Raises:
RuntimeError – if no lock is currently held and force is
False- Return type:
- class filelock.AsyncSoftFileLease(lock_file, *, lease_duration=30.0, heartbeat_interval=None, on_compromise=None, **kwargs)[source]¶
Bases:
SoftFileLease,BaseAsyncFileLockExistence lock whose claim expires, so a peer may take it while the previous holder still runs.
Create a lease.
- Parameters:
lease_duration (
float) – seconds of marker staleness after which a contender may take the claim. Every contender for the path must pass the same value.heartbeat_interval (
float|None) – seconds between refreshes. Defaults to a third oflease_duration, leaving room for two missed refreshes before a peer may take the claim. Must be shorter thanlease_duration.on_compromise (
Callable[[LeaseCompromise],None] |None) – called from the heartbeat thread with aLeaseCompromisewhen the claim is lost.kwargs (
Unpack[LockOptions]) – every otherBaseFileLockoption,timeoutandmodeamong them. The metaclass passes them all by keyword, and taking them here letsAsyncSoftFileLeaseadd the async plumbing a fixed signature would hide.
- class filelock.AsyncSoftFileLock(lock_file, timeout=-1, mode=-1, thread_local=False, *, blocking=True, is_singleton=False, poll_interval=0.05, lifetime=None, context_error_policy='chain', close_error_policy='default', fallback_to_soft=True, preserve_lock_file=False, on_acquired=None, loop=None, run_in_executor=True, executor=None)[source]¶
Bases:
SoftFileLock,BaseAsyncFileLockSimply watches the existence of the lock file.
Create a new lock object.
- Parameters:
timeout (
float) – default timeout when acquiring the lock, in seconds. It will be used as fallback value in the acquire method, if no timeout value (None) is given. If you want to disable the timeout, set it to a negative value. A timeout of 0 means that there is exactly one attempt to acquire the file lock.mode (
int) – file permissions for the lockfile. When not specified, the OS controls permissions via umask and default ACLs, preserving POSIX default ACL inheritance in shared directories.thread_local (
bool) – Whether this object’s internal context should be thread local or not. If this is set toFalsethen the lock will be reentrant across threads. WhenTrue(the default), all fields of the lock’s internal context are per-thread, including the configuration valuespoll_interval,timeout,blocking,mode, andlifetime. Setting one of these properties from one thread does not change the value seen by another thread; threads that did not perform the write continue to see the value supplied at construction time.modehas no setter, so construction is the only place it is ever set. If you need configuration values to be visible across threads, construct the lock withthread_local=False.blocking (
bool) – whether the lock should be blocking or notis_singleton (
bool) – If this is set toTruethen only one instance of this class will be created per lock file. This is useful if you want to use the lock object for reentrant locking without needing to pass the same object around.poll_interval (
float) – default interval for polling the lock file, in seconds. It will be used as fallback value in the acquire method, if no poll_interval value (None) is given.lifetime (
float|None) – forAsyncSoftFileLock, the age in seconds after which a waiting process may delete the marker, even while its holder remains alive. This legacy expiry mode does not provide strict mutual exclusion.None(the default) disables age-based expiry. Native OS locks (AsyncFileLock) cannot be revoked by file age and ignore a non-Nonelifetimewith a warning.context_error_policy (
Literal['chain','group']) – how a context manager reconciles a failure in its body with a failure while releasing on exit."chain"(the default) keeps Python’s behavior: the release error propagates with the body error in its__context__."group"raises aBaseExceptionGroupholding the body error first and the release error second, so neither hides the other.close_error_policy (
Literal['default','raise','suppress']) – for native locks (AsyncFileLock), what to do with anos.closefailure after the OS unlock has already committed."default"keeps each platform’s historical behavior,"raise"always propagates theOSError, and"suppress"always ignores it.fallback_to_soft (
bool) – forAsyncFileLock, whether to fall back to soft existence locking whenflockreturnsENOSYS.True(default) keeps the fallback;Falsepropagates the error.preserve_lock_file (
bool) – for native locks (AsyncFileLock), whether filelock promises not to unlink the lock pathname on release.False(default) keeps each backend’s cleanup;Truekeeps a stable file identity (Windows skips its unlink, Unix refuses theENOSYSsoft fallback).AsyncSoftFileLockrejectsTrue.on_acquired (
Callable[[int],None] |None) – for native locks (AsyncFileLock), a callable invoked with the borrowed lock descriptor once per physical acquisition, after the lock is held but beforeacquire()returns. Withrun_in_executor=True(the default) it runs in the backend executor. It must not close or unlock the descriptor; a raise rolls the acquisition back.AsyncSoftFileLockrejects it.loop (
AbstractEventLoop|None) – The event loop to use. If not specified, the running event loop will be used.run_in_executor (
bool) – If this is set toTruethen the lock will be acquired in an executor.executor (
Executor|None) – The executor to use. If not specified, the default executor will be used.
- class filelock.AsyncSoftReadWriteLock(lock_file, timeout=-1, *, blocking=True, is_singleton=True, heartbeat_interval=30.0, stale_threshold=None, poll_interval=0.25, loop=None, executor=None)[source]¶
Bases:
objectAsync wrapper around
SoftReadWriteLockforasyncioapplications.The sync class’s blocking filesystem operations run on a thread pool via
loop.run_in_executor(). The underlyingSoftReadWriteLockhandles reentrancy, upgrade/downgrade rules, fork handling, heartbeat and TTL stale detection, and singleton behavior.- Parameters:
lock_file (
str|PathLike[str]) – path to the lock file; sidecar state/write/readers live next to ittimeout (
float) – maximum wait time in seconds;-1means block indefinitelyblocking (
bool) – ifFalse, raiseTimeoutimmediately on contentionis_singleton (
bool) – ifTrue, reuse existingSoftReadWriteLockinstances per resolved pathheartbeat_interval (
float) – seconds between heartbeat refreshes; default 30 sstale_threshold (
float|None) – seconds of mtime inactivity before a marker is stale; defaults to3 * heartbeat_intervalpoll_interval (
float) – seconds between acquire retries under contention; default 0.25 sloop (
AbstractEventLoop|None) – event loop forrun_in_executor;Noneuses the running loopexecutor (
Executor|None) – executor forrun_in_executor;Noneuses the default executor
Added in version 3.27.0.
- property lock_file¶
The path to the lock file passed to the constructor.
- property timeout¶
The default timeout applied when
acquire_read/acquire_writeis called without one.
- property blocking¶
Whether
acquire_*defaults to blocking;Falsemakes contention raise immediately.
- property loop¶
The event loop used for
run_in_executor, orNonefor the running loop.
- property executor¶
The executor used for
run_in_executor, orNonefor the default executor.
- read_lock(timeout=None, *, blocking=None)[source]¶
Async context manager that acquires and releases a shared read lock.
- Parameters:
- Raises:
RuntimeError – if a write lock is already held on this instance
Timeout – if the lock cannot be acquired within timeout seconds
- Return type:
- write_lock(timeout=None, *, blocking=None)[source]¶
Async context manager that acquires and releases an exclusive write lock.
- Parameters:
- Raises:
RuntimeError – if a read lock is already held, or a write lock is held by a different thread
Timeout – if the lock cannot be acquired within timeout seconds
- Return type:
- async acquire_read(timeout=None, *, blocking=None)[source]¶
Acquire a shared read lock.
See
SoftReadWriteLock.acquire_read()for reentrancy / upgrade / fork semantics. The blocking work runs insiderun_in_executorso other coroutines on the same loop keep progressing while this call waits.- Parameters:
- Return type:
- Returns:
a proxy usable as an async context manager to release the lock
- Raises:
RuntimeError – if a write lock is already held, if this instance was invalidated by
os.fork(), or ifclose()was calledTimeout – if the lock cannot be acquired within timeout seconds
- async acquire_write(timeout=None, *, blocking=None)[source]¶
Acquire an exclusive write lock.
See
SoftReadWriteLock.acquire_write()for the two-phase writer-preferring semantics. The blocking work runs insiderun_in_executor.- Parameters:
- Return type:
- Returns:
a proxy usable as an async context manager to release the lock
- Raises:
RuntimeError – if a read lock is already held, if a write lock is held by a different thread, if this instance was invalidated by
os.fork(), or ifclose()was calledTimeout – if the lock cannot be acquired within timeout seconds
- async release(*, force=False)[source]¶
Release one level of the current lock.
- Parameters:
force (
bool) – ifTrue, release the lock completely regardless of the current lock level- Raises:
RuntimeError – if no lock is currently held and force is
False- Return type:
- class filelock.AsyncStrictSoftFileLock(lock_file, timeout=-1, mode=-1, thread_local=False, *, blocking=True, is_singleton=False, poll_interval=0.05, lifetime=None, context_error_policy='chain', close_error_policy='default', fallback_to_soft=True, preserve_lock_file=False, on_acquired=None, loop=None, run_in_executor=True, executor=None)[source]¶
Bases:
StrictSoftFileLock,BaseAsyncFileLockRun strict owner-claim locking without blocking the event loop.
Create a new lock object.
- Parameters:
timeout (
float) – default timeout when acquiring the lock, in seconds. It will be used as fallback value in the acquire method, if no timeout value (None) is given. If you want to disable the timeout, set it to a negative value. A timeout of 0 means that there is exactly one attempt to acquire the file lock.mode (
int) – file permissions for the lockfile. When not specified, the OS controls permissions via umask and default ACLs, preserving POSIX default ACL inheritance in shared directories.thread_local (
bool) – Whether this object’s internal context should be thread local or not. If this is set toFalsethen the lock will be reentrant across threads. WhenTrue(the default), all fields of the lock’s internal context are per-thread, including the configuration valuespoll_interval,timeout,blocking,mode, andlifetime. Setting one of these properties from one thread does not change the value seen by another thread; threads that did not perform the write continue to see the value supplied at construction time.modehas no setter, so construction is the only place it is ever set. If you need configuration values to be visible across threads, construct the lock withthread_local=False.blocking (
bool) – whether the lock should be blocking or notis_singleton (
bool) – If this is set toTruethen only one instance of this class will be created per lock file. This is useful if you want to use the lock object for reentrant locking without needing to pass the same object around.poll_interval (
float) – default interval for polling the lock file, in seconds. It will be used as fallback value in the acquire method, if no poll_interval value (None) is given.lifetime (
float|None) – forAsyncSoftFileLock, the age in seconds after which a waiting process may delete the marker, even while its holder remains alive. This legacy expiry mode does not provide strict mutual exclusion.None(the default) disables age-based expiry. Native OS locks (AsyncFileLock) cannot be revoked by file age and ignore a non-Nonelifetimewith a warning.context_error_policy (
Literal['chain','group']) – how a context manager reconciles a failure in its body with a failure while releasing on exit."chain"(the default) keeps Python’s behavior: the release error propagates with the body error in its__context__."group"raises aBaseExceptionGroupholding the body error first and the release error second, so neither hides the other.close_error_policy (
Literal['default','raise','suppress']) – for native locks (AsyncFileLock), what to do with anos.closefailure after the OS unlock has already committed."default"keeps each platform’s historical behavior,"raise"always propagates theOSError, and"suppress"always ignores it.fallback_to_soft (
bool) – forAsyncFileLock, whether to fall back to soft existence locking whenflockreturnsENOSYS.True(default) keeps the fallback;Falsepropagates the error.preserve_lock_file (
bool) – for native locks (AsyncFileLock), whether filelock promises not to unlink the lock pathname on release.False(default) keeps each backend’s cleanup;Truekeeps a stable file identity (Windows skips its unlink, Unix refuses theENOSYSsoft fallback).AsyncSoftFileLockrejectsTrue.on_acquired (
Callable[[int],None] |None) – for native locks (AsyncFileLock), a callable invoked with the borrowed lock descriptor once per physical acquisition, after the lock is held but beforeacquire()returns. Withrun_in_executor=True(the default) it runs in the backend executor. It must not close or unlock the descriptor; a raise rolls the acquisition back.AsyncSoftFileLockrejects it.loop (
AbstractEventLoop|None) – The event loop to use. If not specified, the running event loop will be used.run_in_executor (
bool) – If this is set toTruethen the lock will be acquired in an executor.executor (
Executor|None) – The executor to use. If not specified, the default executor will be used.
- class filelock.AsyncUnixFileLock(lock_file, timeout=-1, mode=-1, thread_local=False, *, blocking=True, is_singleton=False, poll_interval=0.05, lifetime=None, context_error_policy='chain', close_error_policy='default', fallback_to_soft=True, preserve_lock_file=False, on_acquired=None, loop=None, run_in_executor=True, executor=None)[source]¶
Bases:
UnixFileLock,BaseAsyncFileLockUses the
fcntl.flock()to hard lock the lock file on unix systems.Create a new lock object.
- Parameters:
timeout (
float) – default timeout when acquiring the lock, in seconds. It will be used as fallback value in the acquire method, if no timeout value (None) is given. If you want to disable the timeout, set it to a negative value. A timeout of 0 means that there is exactly one attempt to acquire the file lock.mode (
int) – file permissions for the lockfile. When not specified, the OS controls permissions via umask and default ACLs, preserving POSIX default ACL inheritance in shared directories.thread_local (
bool) – Whether this object’s internal context should be thread local or not. If this is set toFalsethen the lock will be reentrant across threads. WhenTrue(the default), all fields of the lock’s internal context are per-thread, including the configuration valuespoll_interval,timeout,blocking,mode, andlifetime. Setting one of these properties from one thread does not change the value seen by another thread; threads that did not perform the write continue to see the value supplied at construction time.modehas no setter, so construction is the only place it is ever set. If you need configuration values to be visible across threads, construct the lock withthread_local=False.blocking (
bool) – whether the lock should be blocking or notis_singleton (
bool) – If this is set toTruethen only one instance of this class will be created per lock file. This is useful if you want to use the lock object for reentrant locking without needing to pass the same object around.poll_interval (
float) – default interval for polling the lock file, in seconds. It will be used as fallback value in the acquire method, if no poll_interval value (None) is given.lifetime (
float|None) – forAsyncSoftFileLock, the age in seconds after which a waiting process may delete the marker, even while its holder remains alive. This legacy expiry mode does not provide strict mutual exclusion.None(the default) disables age-based expiry. Native OS locks (AsyncFileLock) cannot be revoked by file age and ignore a non-Nonelifetimewith a warning.context_error_policy (
Literal['chain','group']) – how a context manager reconciles a failure in its body with a failure while releasing on exit."chain"(the default) keeps Python’s behavior: the release error propagates with the body error in its__context__."group"raises aBaseExceptionGroupholding the body error first and the release error second, so neither hides the other.close_error_policy (
Literal['default','raise','suppress']) – for native locks (AsyncFileLock), what to do with anos.closefailure after the OS unlock has already committed."default"keeps each platform’s historical behavior,"raise"always propagates theOSError, and"suppress"always ignores it.fallback_to_soft (
bool) – forAsyncFileLock, whether to fall back to soft existence locking whenflockreturnsENOSYS.True(default) keeps the fallback;Falsepropagates the error.preserve_lock_file (
bool) – for native locks (AsyncFileLock), whether filelock promises not to unlink the lock pathname on release.False(default) keeps each backend’s cleanup;Truekeeps a stable file identity (Windows skips its unlink, Unix refuses theENOSYSsoft fallback).AsyncSoftFileLockrejectsTrue.on_acquired (
Callable[[int],None] |None) – for native locks (AsyncFileLock), a callable invoked with the borrowed lock descriptor once per physical acquisition, after the lock is held but beforeacquire()returns. Withrun_in_executor=True(the default) it runs in the backend executor. It must not close or unlock the descriptor; a raise rolls the acquisition back.AsyncSoftFileLockrejects it.loop (
AbstractEventLoop|None) – The event loop to use. If not specified, the running event loop will be used.run_in_executor (
bool) – If this is set toTruethen the lock will be acquired in an executor.executor (
Executor|None) – The executor to use. If not specified, the default executor will be used.
- class filelock.AsyncWindowsFileLock(lock_file, timeout=-1, mode=-1, thread_local=False, *, blocking=True, is_singleton=False, poll_interval=0.05, lifetime=None, context_error_policy='chain', close_error_policy='default', fallback_to_soft=True, preserve_lock_file=False, on_acquired=None, loop=None, run_in_executor=True, executor=None)[source]¶
Bases:
WindowsFileLock,BaseAsyncFileLockUses the
msvcrt.locking()to hard lock the lock file on windows systems.Create a new lock object.
- Parameters:
timeout (
float) – default timeout when acquiring the lock, in seconds. It will be used as fallback value in the acquire method, if no timeout value (None) is given. If you want to disable the timeout, set it to a negative value. A timeout of 0 means that there is exactly one attempt to acquire the file lock.mode (
int) – file permissions for the lockfile. When not specified, the OS controls permissions via umask and default ACLs, preserving POSIX default ACL inheritance in shared directories.thread_local (
bool) – Whether this object’s internal context should be thread local or not. If this is set toFalsethen the lock will be reentrant across threads. WhenTrue(the default), all fields of the lock’s internal context are per-thread, including the configuration valuespoll_interval,timeout,blocking,mode, andlifetime. Setting one of these properties from one thread does not change the value seen by another thread; threads that did not perform the write continue to see the value supplied at construction time.modehas no setter, so construction is the only place it is ever set. If you need configuration values to be visible across threads, construct the lock withthread_local=False.blocking (
bool) – whether the lock should be blocking or notis_singleton (
bool) – If this is set toTruethen only one instance of this class will be created per lock file. This is useful if you want to use the lock object for reentrant locking without needing to pass the same object around.poll_interval (
float) – default interval for polling the lock file, in seconds. It will be used as fallback value in the acquire method, if no poll_interval value (None) is given.lifetime (
float|None) – forAsyncSoftFileLock, the age in seconds after which a waiting process may delete the marker, even while its holder remains alive. This legacy expiry mode does not provide strict mutual exclusion.None(the default) disables age-based expiry. Native OS locks (AsyncFileLock) cannot be revoked by file age and ignore a non-Nonelifetimewith a warning.context_error_policy (
Literal['chain','group']) – how a context manager reconciles a failure in its body with a failure while releasing on exit."chain"(the default) keeps Python’s behavior: the release error propagates with the body error in its__context__."group"raises aBaseExceptionGroupholding the body error first and the release error second, so neither hides the other.close_error_policy (
Literal['default','raise','suppress']) – for native locks (AsyncFileLock), what to do with anos.closefailure after the OS unlock has already committed."default"keeps each platform’s historical behavior,"raise"always propagates theOSError, and"suppress"always ignores it.fallback_to_soft (
bool) – forAsyncFileLock, whether to fall back to soft existence locking whenflockreturnsENOSYS.True(default) keeps the fallback;Falsepropagates the error.preserve_lock_file (
bool) – for native locks (AsyncFileLock), whether filelock promises not to unlink the lock pathname on release.False(default) keeps each backend’s cleanup;Truekeeps a stable file identity (Windows skips its unlink, Unix refuses theENOSYSsoft fallback).AsyncSoftFileLockrejectsTrue.on_acquired (
Callable[[int],None] |None) – for native locks (AsyncFileLock), a callable invoked with the borrowed lock descriptor once per physical acquisition, after the lock is held but beforeacquire()returns. Withrun_in_executor=True(the default) it runs in the backend executor. It must not close or unlock the descriptor; a raise rolls the acquisition back.AsyncSoftFileLockrejects it.loop (
AbstractEventLoop|None) – The event loop to use. If not specified, the running event loop will be used.run_in_executor (
bool) – If this is set toTruethen the lock will be acquired in an executor.executor (
Executor|None) – The executor to use. If not specified, the default executor will be used.
- class filelock.BaseAsyncFileLock(lock_file, timeout=-1, mode=-1, thread_local=False, *, blocking=True, is_singleton=False, poll_interval=0.05, lifetime=None, context_error_policy='chain', close_error_policy='default', fallback_to_soft=True, preserve_lock_file=False, on_acquired=None, loop=None, run_in_executor=True, executor=None)[source]¶
Bases:
BaseFileLockBase class for asynchronous file locks.
Added in version 3.15.0.
Create a new lock object.
- Parameters:
timeout (
float) – default timeout when acquiring the lock, in seconds. It will be used as fallback value in the acquire method, if no timeout value (None) is given. If you want to disable the timeout, set it to a negative value. A timeout of 0 means that there is exactly one attempt to acquire the file lock.mode (
int) – file permissions for the lockfile. When not specified, the OS controls permissions via umask and default ACLs, preserving POSIX default ACL inheritance in shared directories.thread_local (
bool) – Whether this object’s internal context should be thread local or not. If this is set toFalsethen the lock will be reentrant across threads. WhenTrue(the default), all fields of the lock’s internal context are per-thread, including the configuration valuespoll_interval,timeout,blocking,mode, andlifetime. Setting one of these properties from one thread does not change the value seen by another thread; threads that did not perform the write continue to see the value supplied at construction time.modehas no setter, so construction is the only place it is ever set. If you need configuration values to be visible across threads, construct the lock withthread_local=False.blocking (
bool) – whether the lock should be blocking or notis_singleton (
bool) – If this is set toTruethen only one instance of this class will be created per lock file. This is useful if you want to use the lock object for reentrant locking without needing to pass the same object around.poll_interval (
float) – default interval for polling the lock file, in seconds. It will be used as fallback value in the acquire method, if no poll_interval value (None) is given.lifetime (
float|None) – forAsyncSoftFileLock, the age in seconds after which a waiting process may delete the marker, even while its holder remains alive. This legacy expiry mode does not provide strict mutual exclusion.None(the default) disables age-based expiry. Native OS locks (AsyncFileLock) cannot be revoked by file age and ignore a non-Nonelifetimewith a warning.context_error_policy (
Literal['chain','group']) – how a context manager reconciles a failure in its body with a failure while releasing on exit."chain"(the default) keeps Python’s behavior: the release error propagates with the body error in its__context__."group"raises aBaseExceptionGroupholding the body error first and the release error second, so neither hides the other.close_error_policy (
Literal['default','raise','suppress']) – for native locks (AsyncFileLock), what to do with anos.closefailure after the OS unlock has already committed."default"keeps each platform’s historical behavior,"raise"always propagates theOSError, and"suppress"always ignores it.fallback_to_soft (
bool) – forAsyncFileLock, whether to fall back to soft existence locking whenflockreturnsENOSYS.True(default) keeps the fallback;Falsepropagates the error.preserve_lock_file (
bool) – for native locks (AsyncFileLock), whether filelock promises not to unlink the lock pathname on release.False(default) keeps each backend’s cleanup;Truekeeps a stable file identity (Windows skips its unlink, Unix refuses theENOSYSsoft fallback).AsyncSoftFileLockrejectsTrue.on_acquired (
Callable[[int],None] |None) – for native locks (AsyncFileLock), a callable invoked with the borrowed lock descriptor once per physical acquisition, after the lock is held but beforeacquire()returns. Withrun_in_executor=True(the default) it runs in the backend executor. It must not close or unlock the descriptor; a raise rolls the acquisition back.AsyncSoftFileLockrejects it.loop (
AbstractEventLoop|None) – The event loop to use. If not specified, the running event loop will be used.run_in_executor (
bool) – If this is set toTruethen the lock will be acquired in an executor.executor (
Executor|None) – The executor to use. If not specified, the default executor will be used.
- property run_in_executor¶
Whether run in executor.
- property executor¶
The executor.
- property loop¶
The event loop.
- async acquire(timeout=None, poll_interval=None, *, blocking=None, cancel_check=None)[source]¶
Try to acquire the file lock.
- Parameters:
timeout (
float|None) – maximum wait time for acquiring the lock,Nonemeans use the defaulttimeoutis and iftimeout < 0, there is no timeout and this method will block until the lock could be acquiredpoll_interval (
float|None) – interval of trying to acquire the lock file,Nonemeans use the defaultpoll_intervalblocking (
bool|None) – defaults to True. If False, function will return immediately if it cannot obtain a lock on the first attempt. Otherwise, this method will block until the timeout expires or the lock is acquired.cancel_check (
Callable[[],bool] |None) – a callable returningTruewhen the acquisition should be canceled. Checked on each poll iteration. When triggered, raisesTimeoutjust like an expired timeout.
- Return type:
- Returns:
a context object that will unlock the file when the context is exited
- Raises:
Timeout – if fails to acquire lock within the timeout period
# You can use this method in the context manager (recommended) with lock.acquire(): pass # Or use an equivalent try-finally construct: lock.acquire() try: pass finally: lock.release()
- class filelock.BaseFileLock(lock_file, timeout=-1, mode=-1, thread_local=True, *, blocking=True, is_singleton=False, poll_interval=0.05, lifetime=None, context_error_policy='chain', close_error_policy='default', fallback_to_soft=True, preserve_lock_file=False, on_acquired=None)[source]¶
Bases:
ContextDecoratorAbstract base class for a file lock object.
Provides the common reentrant API and state management. Subclasses implement the locking mechanism (
UnixFileLock,WindowsFileLock,SoftFileLock).Create a new lock object.
- Parameters:
timeout (
float) – default timeout when acquiring the lock, in seconds. It will be used as fallback value in the acquire method, if no timeout value (None) is given. If you want to disable the timeout, set it to a negative value. A timeout of 0 means that there is exactly one attempt to acquire the file lock.mode (
int) – file permissions for the lockfile. When not specified, the OS controls permissions via umask and default ACLs, preserving POSIX default ACL inheritance in shared directories.thread_local (
bool) – Whether this object’s internal context should be thread local or not. If this is set toFalsethen the lock will be reentrant across threads. WhenTrue(the default), all fields of the lock’s internal context are per-thread, including the configuration valuespoll_interval,timeout,blocking,mode, andlifetime. Setting one of these properties from one thread does not change the value seen by another thread; threads that did not perform the write continue to see the value supplied at construction time.modehas no setter, so construction is the only place it is ever set. If you need configuration values to be visible across threads, construct the lock withthread_local=False.blocking (
bool) – whether the lock should be blocking or notis_singleton (
bool) – If this is set toTruethen only one instance of this class will be created per lock file. This is useful if you want to use the lock object for reentrant locking without needing to pass the same object around.poll_interval (
float) – default interval for polling the lock file, in seconds. It will be used as fallback value in the acquire method, if no poll_interval value (None) is given.lifetime (
float|None) – forSoftFileLock, the age in seconds after which a waiting process may delete the marker, even while its holder remains alive. This legacy expiry mode does not provide strict mutual exclusion.None(the default) disables age-based expiry. Native OS locks (FileLock) cannot be revoked by file age and ignore a non-Nonelifetimewith a warning.context_error_policy (
Literal['chain','group']) – how a context manager reconciles a failure in its body with a failure while releasing on exit."chain"(the default) keeps Python’s behavior: the release error propagates with the body error in its__context__."group"raises aBaseExceptionGroupholding the body error first and the release error second, so neither hides the other.close_error_policy (
Literal['default','raise','suppress']) – what to do with anos.closefailure after relinquishing descriptor ownership."default"keeps each backend’s historical behavior (Unix native locks drop a FUSE/DockerEIO; Windows native locks andSoftFileLockpropagate);"raise"always propagates theOSError;"suppress"always ignores it. Held state is released either way. It does not affect unlock failures or lock-file deletion.fallback_to_soft (
bool) – forUnixFileLock, whether to switch toSoftFileLockwhen the filesystem’sflockreturnsENOSYS.True(the default) keeps the historical fallback;Falsefails closed, letting theENOSYSpropagate so a caller that needs kernel-enforced locking is never silently downgraded. It has no effect on Windows orSoftFileLock.preserve_lock_file (
bool) – for native locks (FileLock), whether filelock promises not to unlink the lock pathname on release.False(the default) keeps each backend’s cleanup: Windows removes the lock file, Unix already leaves it.Truekeeps a stable file identity for ACLs, auditing, and holder metadata: Windows skips its post-release unlink and Unix refuses to enter theENOSYSsoft fallback (which releases by unlinking).SoftFileLockrejectsTrue. The promise covers filelock’s own release path only; it cannot stop another process or the filesystem from removing the pathname.on_acquired (
Callable[[int],None] |None) – for native locks (FileLock), a callable invoked with the borrowed lock descriptor once per physical acquisition, after filelock holds the native lock and finished backend initialization but beforeacquire()returns. Recursive acquisitions do not call it again. The callback may read, write, seek, truncate, or set metadata throughoson the descriptor, but must not close, unlock, or take ownership of it, and filelock does not fsync its writes. If it raises, filelock releases the lock and re-raises.SoftFileLockrejects the hook.
- is_thread_local()[source]¶
- Return type:
- Returns:
a flag indicating if this lock is thread local or not
- property is_singleton¶
A flag indicating if this lock is singleton or not.
Added in version 3.13.0.
- property context_error_policy¶
How a context manager reconciles a body failure with a release failure on exit.
Added in version 3.30.0.
- property close_error_policy¶
What a lock does with an
os.closefailure after relinquishing descriptor ownership.Added in version 3.30.0.
- property fallback_to_soft¶
Whether a
FileLockfalls back toSoftFileLockwhen the filesystem lacksflock.Only
UnixFileLockacts on it: whenFalseanENOSYSfromflockpropagates instead of switching to existence-lock semantics.Added in version 3.30.0.
- property preserve_lock_file¶
Whether filelock promises not to unlink the lock pathname on release.
When
True, Windows skips its post-release unlink and Unix refuses theENOSYSsoft fallback.SoftFileLockrejectsTruebecause unlinking its marker is how it releases.Added in version 3.30.0.
- property on_acquired¶
The callback run with the borrowed lock descriptor once per physical acquisition, or
None.Native locks only. It runs after the native lock is held and backend initialization finished, before
acquire()returns; a raise rolls back the acquisition.SoftFileLockrejects it.Added in version 3.30.0.
- property lock_file¶
Path to the lock file.
- property timeout¶
The default timeout value, in seconds.
Added in version 2.0.0.
- property blocking¶
Whether the locking is blocking or not.
Added in version 3.14.0.
- property poll_interval¶
The default polling interval, in seconds.
Added in version 3.24.0.
- property lifetime¶
The soft marker age in seconds that permits expiry, or
Noneto disable age-based expiry.A non-
Nonevalue permits a waiter to enter while the previous holder remains active, so it does not provide strict mutual exclusion. Native locks ignore the value with a warning.Added in version 3.24.0.
- property mode¶
The file permissions for the lockfile.
- property has_explicit_mode¶
Whether the file permissions were explicitly set.
- property is_locked¶
A boolean indicating if the lock file is holding the lock currently.
Changed in version 2.0.0: This was previously a method and is now a property.
- property lock_counter¶
The number of times this lock has been acquired (but not yet released).
- acquire(timeout=None, poll_interval=None, *, poll_intervall=None, blocking=None, cancel_check=None)[source]¶
Try to acquire the file lock.
- Parameters:
timeout (
float|None) – maximum wait time for acquiring the lock,Nonemeans use the defaulttimeoutis and iftimeout < 0, there is no timeout and this method will block until the lock could be acquiredpoll_interval (
float|None) – interval of trying to acquire the lock file,Nonemeans use the defaultpoll_intervalpoll_intervall (
float|None) – deprecated, kept for backwards compatibility, usepoll_intervalinsteadblocking (
bool|None) – defaults to True. If False, function will return immediately if it cannot obtain a lock on the first attempt. Otherwise, this method will block until the timeout expires or the lock is acquired.cancel_check (
Callable[[],bool] |None) – a callable returningTruewhen the acquisition should be canceled. Checked on each poll iteration. When triggered, raisesTimeoutjust like an expired timeout.
- Return type:
- Returns:
a context object that will unlock the file when the context is exited
- Raises:
Timeout – if fails to acquire lock within the timeout period
# You can use this method in the context manager (recommended) with lock.acquire(): pass # Or use an equivalent try-finally construct: lock.acquire() try: pass finally: lock.release()
Changed in version 2.0.0: This method returns now a proxy object instead of self, so that it can be used in a with statement without side effects.
- filelock.FileLock¶
Alias for the lock, which should be used for the current platform.
- class filelock.LeaseCompromise(lock_file, token, reason, error=None)[source]¶
Bases:
objectWhy a held lease stopped being this process’s to hold.
- lock_file¶
- token¶
- reason¶
- error = None¶
- exception filelock.LeaseSettingsMismatch[source]¶
Bases:
ValueErrorA lease contender disagrees with the published claim about how long the lease lasts.
- class filelock.LockOptions[source]¶
Bases:
TypedDictEvery option the metaclass forwards, so a subclass adding its own can still type what it passes through.
- timeout¶
- mode¶
- thread_local¶
- blocking¶
- is_singleton¶
- poll_interval¶
- lifetime¶
- context_error_policy¶
- close_error_policy¶
- fallback_to_soft¶
- preserve_lock_file¶
- on_acquired¶
- class filelock.MarkerSoftFileLock(lock_file, timeout=-1, mode=-1, thread_local=True, *, blocking=True, is_singleton=False, poll_interval=0.05, lifetime=None, context_error_policy='chain', close_error_policy='default', fallback_to_soft=True, preserve_lock_file=False, on_acquired=None)[source]¶
Bases:
SoftFileLockAn existence lock whose marker carries a protocol 2 owner record.
Create a new lock object.
- Parameters:
timeout (
float) – default timeout when acquiring the lock, in seconds. It will be used as fallback value in the acquire method, if no timeout value (None) is given. If you want to disable the timeout, set it to a negative value. A timeout of 0 means that there is exactly one attempt to acquire the file lock.mode (
int) – file permissions for the lockfile. When not specified, the OS controls permissions via umask and default ACLs, preserving POSIX default ACL inheritance in shared directories.thread_local (
bool) – Whether this object’s internal context should be thread local or not. If this is set toFalsethen the lock will be reentrant across threads. WhenTrue(the default), all fields of the lock’s internal context are per-thread, including the configuration valuespoll_interval,timeout,blocking,mode, andlifetime. Setting one of these properties from one thread does not change the value seen by another thread; threads that did not perform the write continue to see the value supplied at construction time.modehas no setter, so construction is the only place it is ever set. If you need configuration values to be visible across threads, construct the lock withthread_local=False.blocking (
bool) – whether the lock should be blocking or notis_singleton (
bool) – If this is set toTruethen only one instance of this class will be created per lock file. This is useful if you want to use the lock object for reentrant locking without needing to pass the same object around.poll_interval (
float) – default interval for polling the lock file, in seconds. It will be used as fallback value in the acquire method, if no poll_interval value (None) is given.lifetime (
float|None) – forSoftFileLock, the age in seconds after which a waiting process may delete the marker, even while its holder remains alive. This legacy expiry mode does not provide strict mutual exclusion.None(the default) disables age-based expiry. Native OS locks (FileLock) cannot be revoked by file age and ignore a non-Nonelifetimewith a warning.context_error_policy (
Literal['chain','group']) – how a context manager reconciles a failure in its body with a failure while releasing on exit."chain"(the default) keeps Python’s behavior: the release error propagates with the body error in its__context__."group"raises aBaseExceptionGroupholding the body error first and the release error second, so neither hides the other.close_error_policy (
Literal['default','raise','suppress']) – what to do with anos.closefailure after relinquishing descriptor ownership."default"keeps each backend’s historical behavior (Unix native locks drop a FUSE/DockerEIO; Windows native locks andSoftFileLockpropagate);"raise"always propagates theOSError;"suppress"always ignores it. Held state is released either way. It does not affect unlock failures or lock-file deletion.fallback_to_soft (
bool) – forUnixFileLock, whether to switch toSoftFileLockwhen the filesystem’sflockreturnsENOSYS.True(the default) keeps the historical fallback;Falsefails closed, letting theENOSYSpropagate so a caller that needs kernel-enforced locking is never silently downgraded. It has no effect on Windows orSoftFileLock.preserve_lock_file (
bool) – for native locks (FileLock), whether filelock promises not to unlink the lock pathname on release.False(the default) keeps each backend’s cleanup: Windows removes the lock file, Unix already leaves it.Truekeeps a stable file identity for ACLs, auditing, and holder metadata: Windows skips its post-release unlink and Unix refuses to enter theENOSYSsoft fallback (which releases by unlinking).SoftFileLockrejectsTrue. The promise covers filelock’s own release path only; it cannot stop another process or the filesystem from removing the pathname.on_acquired (
Callable[[int],None] |None) – for native locks (FileLock), a callable invoked with the borrowed lock descriptor once per physical acquisition, after filelock holds the native lock and finished backend initialization but beforeacquire()returns. Recursive acquisitions do not call it again. The callback may read, write, seek, truncate, or set metadata throughoson the descriptor, but must not close, unlock, or take ownership of it, and filelock does not fsync its writes. If it raises, filelock releases the lock and re-raises.SoftFileLockrejects the hook.
- property owner¶
The owner named by the marker on disk.
- Returns:
the published record, or
Nonewhen no marker exists or its record is malformed or protocol 1
- property pid¶
The PID of the process holding this lock, read from the marker.
- Returns:
the PID, or
Nonewhen no marker exists or its record is unreadable
- property is_lock_held_by_us¶
Whether the marker on disk names this process.
- Returns:
Truewhen the marker’s PID and hostname match this process
- force_break()[source]¶
Remove the marker whoever holds it, so a later contender can acquire.
Forced breaking voids mutual exclusion: the previous holder keeps running and keeps using whatever the lock protects. Reserve it for an operator clearing a marker whose holder is known to be gone.
- Return type:
- class filelock.OwnerRecord(pid, hostname, mode, token=None, lease_duration=None, start=None)[source]¶
Bases:
NamedTupleThe owner published in a protocol 2 marker.
Create new instance of OwnerRecord(pid, hostname, mode, token, lease_duration, start)
- pid¶
Alias for field number 0
- hostname¶
Alias for field number 1
- mode¶
Alias for field number 2
- token¶
Alias for field number 3
- lease_duration¶
Alias for field number 4
- start¶
Alias for field number 5
- class filelock.ReadWriteLock(lock_file, timeout=-1, *, blocking=True, is_singleton=True)[source]¶
Bases:
objectCross-process read-write lock backed by SQLite.
Allows concurrent shared readers or a single exclusive writer. The lock is reentrant within the same mode (multiple
acquire_readcalls nest, as do multipleacquire_writecalls from the same thread), but upgrading from read to write or downgrading from write to read raisesRuntimeError. Write locks are pinned to the thread that acquired them.By default,
is_singleton=True: callingReadWriteLock(path)with the same resolved path returns the same instance. The path is handed tosqlite3.connect()as given, so a.dbextension is a convention rather than a requirement; the filesystem must be one the active SQLite VFS supports.- Parameters:
lock_file (
str|PathLike[str]) – path to the SQLite database file used as the locktimeout (
float) – maximum wait time in seconds;-1means block indefinitelyblocking (
bool) – ifFalse, raiseTimeoutimmediately when the lock is unavailableis_singleton (
bool) – ifTrue, reuse existing instances for the same resolved path
Added in version 3.21.0.
- classmethod get_lock(lock_file, timeout=-1, *, blocking=True)[source]¶
Return the singleton
ReadWriteLockfor lock_file.- Parameters:
- Return type:
- Returns:
the singleton lock instance
- Raises:
ValueError – if an instance already exists for this path with different timeout or blocking values
- acquire_read(timeout=-1, *, blocking=True)[source]¶
Acquire a shared read lock.
If this instance already holds a read lock, the lock level is incremented (reentrant). Attempting to acquire a read lock while holding a write lock raises
RuntimeError(downgrade not allowed).- Parameters:
- Return type:
- Returns:
a proxy that can be used as a context manager to release the lock
- Raises:
RuntimeError – if a write lock is already held on this instance
Timeout – if the lock cannot be acquired within timeout seconds
- acquire_write(timeout=-1, *, blocking=True)[source]¶
Acquire an exclusive write lock.
If this instance already holds a write lock from the same thread, the lock level is incremented (reentrant). Attempting to acquire a write lock while holding a read lock raises
RuntimeError(upgrade not allowed). Write locks are pinned to the acquiring thread: a different thread trying to re-enter also raisesRuntimeError.- Parameters:
- Return type:
- Returns:
a proxy that can be used as a context manager to release the lock
- Raises:
RuntimeError – if a read lock is already held, or a write lock is held by a different thread
Timeout – if the lock cannot be acquired within timeout seconds
- read_lock(timeout=None, *, blocking=None)[source]¶
Context manager that acquires and releases a shared read lock.
Falls back to instance defaults for timeout and blocking when
None.
- write_lock(timeout=None, *, blocking=None)[source]¶
Context manager that acquires and releases an exclusive write lock.
Falls back to instance defaults for timeout and blocking when
None.
- release(*, force=False)[source]¶
Release one level of the current lock.
When the lock level reaches zero the underlying SQLite transaction is rolled back, releasing the database lock.
- Parameters:
force (
bool) – ifTrue, release the lock completely regardless of the current lock level- Raises:
RuntimeError – if no lock is currently held and force is
False- Return type:
- class filelock.SoftFileLease(lock_file, *, lease_duration=30.0, heartbeat_interval=None, on_compromise=None, **kwargs)[source]¶
Bases:
MarkerSoftFileLockExistence lock whose claim expires, so a peer may take it while the previous holder still runs.
A lease trades mutual exclusion for progress. The holder publishes a claim and refreshes it every
heartbeat_intervalseconds; a contender takes the marker once it islease_durationseconds stale. Nothing stops the expired holder: it keeps running, and it keeps using whatever the lock protects. Treat the lease as a hint about who should be working, not as a guarantee that only one worker is.To make a protected resource reject a superseded holder, that resource must be linearizable and must fence on a monotonic generation it controls.
tokennames a claim; it does not fence one. Where overlap is unacceptable, useStrictSoftFileLockinstead.Every contender for a path must agree on
lease_duration. A contender that finds a claim published under a different duration raisesLeaseSettingsMismatchrather than apply its own expiry to a peer that never agreed to it.Expiry reclaims less on Windows, which refuses to rename or delete a file another process holds open. A peer there takes an expired claim only once the previous holder’s process exits and its handle closes; a holder that lives on but stops refreshing keeps the marker. Unix reclaims the marker either way.
on_compromisefires from the heartbeat thread when a refresh fails, or when the marker vanishes or names another owner. The holder should stop touching the protected resource when it runs. Because it runs on that thread, arelease()inside it only takes effect when the lease was built withthread_local=False; the default thread-local context hides the claim from every thread but the one that acquired it, so the release does nothing. Signal the acquiring thread instead when the context stays thread-local.Added in version 3.30.0.
Create a lease.
- Parameters:
lease_duration (
float) – seconds of marker staleness after which a contender may take the claim. Every contender for the path must pass the same value.heartbeat_interval (
float|None) – seconds between refreshes. Defaults to a third oflease_duration, leaving room for two missed refreshes before a peer may take the claim. Must be shorter thanlease_duration.on_compromise (
Callable[[LeaseCompromise],None] |None) – called from the heartbeat thread with aLeaseCompromisewhen the claim is lost.kwargs (
Unpack[LockOptions]) – every otherBaseFileLockoption,timeoutandmodeamong them. The metaclass passes them all by keyword, and taking them here letsAsyncSoftFileLeaseadd the async plumbing a fixed signature would hide.
- property lease_duration¶
The staleness in seconds after which a contender may take this claim.
- property token¶
The token naming the claim this process published.
- Returns:
the token while the lease is held,
Noneotherwise. It identifies a claim; it does not fence one.
- property compromise¶
The loss of claim the heartbeat observed.
- Returns:
the
LeaseCompromise, orNonewhile the claim still holds
- class filelock.SoftFileLock(lock_file, timeout=-1, mode=-1, thread_local=True, *, blocking=True, is_singleton=False, poll_interval=0.05, lifetime=None, context_error_policy='chain', close_error_policy='default', fallback_to_soft=True, preserve_lock_file=False, on_acquired=None)[source]¶
Bases:
BaseFileLockCooperative file lock based on a shared existence marker.
Unlike
UnixFileLockandWindowsFileLock, this lock does not use OS-level locking primitives. Instead, it creates the lock file withO_CREAT | O_EXCLand treats its existence as the lock indicator. The filesystem must provide coherent exclusive creation and directory updates to each participating process. A crash can leave the marker behind.The marker contains the holder’s PID and hostname. A contender may remove it when it can no longer find a same-host process with that PID. A configured
lifetimealso permits removal based on marker age, including while the holder remains alive. Age-based expiry can overlap protected operations and does not provide strict mutual exclusion.Create a new lock object.
- Parameters:
timeout (
float) – default timeout when acquiring the lock, in seconds. It will be used as fallback value in the acquire method, if no timeout value (None) is given. If you want to disable the timeout, set it to a negative value. A timeout of 0 means that there is exactly one attempt to acquire the file lock.mode (
int) – file permissions for the lockfile. When not specified, the OS controls permissions via umask and default ACLs, preserving POSIX default ACL inheritance in shared directories.thread_local (
bool) – Whether this object’s internal context should be thread local or not. If this is set toFalsethen the lock will be reentrant across threads. WhenTrue(the default), all fields of the lock’s internal context are per-thread, including the configuration valuespoll_interval,timeout,blocking,mode, andlifetime. Setting one of these properties from one thread does not change the value seen by another thread; threads that did not perform the write continue to see the value supplied at construction time.modehas no setter, so construction is the only place it is ever set. If you need configuration values to be visible across threads, construct the lock withthread_local=False.blocking (
bool) – whether the lock should be blocking or notis_singleton (
bool) – If this is set toTruethen only one instance of this class will be created per lock file. This is useful if you want to use the lock object for reentrant locking without needing to pass the same object around.poll_interval (
float) – default interval for polling the lock file, in seconds. It will be used as fallback value in the acquire method, if no poll_interval value (None) is given.lifetime (
float|None) – forSoftFileLock, the age in seconds after which a waiting process may delete the marker, even while its holder remains alive. This legacy expiry mode does not provide strict mutual exclusion.None(the default) disables age-based expiry. Native OS locks (FileLock) cannot be revoked by file age and ignore a non-Nonelifetimewith a warning.context_error_policy (
Literal['chain','group']) – how a context manager reconciles a failure in its body with a failure while releasing on exit."chain"(the default) keeps Python’s behavior: the release error propagates with the body error in its__context__."group"raises aBaseExceptionGroupholding the body error first and the release error second, so neither hides the other.close_error_policy (
Literal['default','raise','suppress']) – what to do with anos.closefailure after relinquishing descriptor ownership."default"keeps each backend’s historical behavior (Unix native locks drop a FUSE/DockerEIO; Windows native locks andSoftFileLockpropagate);"raise"always propagates theOSError;"suppress"always ignores it. Held state is released either way. It does not affect unlock failures or lock-file deletion.fallback_to_soft (
bool) – forUnixFileLock, whether to switch toSoftFileLockwhen the filesystem’sflockreturnsENOSYS.True(the default) keeps the historical fallback;Falsefails closed, letting theENOSYSpropagate so a caller that needs kernel-enforced locking is never silently downgraded. It has no effect on Windows orSoftFileLock.preserve_lock_file (
bool) – for native locks (FileLock), whether filelock promises not to unlink the lock pathname on release.False(the default) keeps each backend’s cleanup: Windows removes the lock file, Unix already leaves it.Truekeeps a stable file identity for ACLs, auditing, and holder metadata: Windows skips its post-release unlink and Unix refuses to enter theENOSYSsoft fallback (which releases by unlinking).SoftFileLockrejectsTrue. The promise covers filelock’s own release path only; it cannot stop another process or the filesystem from removing the pathname.on_acquired (
Callable[[int],None] |None) – for native locks (FileLock), a callable invoked with the borrowed lock descriptor once per physical acquisition, after filelock holds the native lock and finished backend initialization but beforeacquire()returns. Recursive acquisitions do not call it again. The callback may read, write, seek, truncate, or set metadata throughoson the descriptor, but must not close, unlock, or take ownership of it, and filelock does not fsync its writes. If it raises, filelock releases the lock and re-raises.SoftFileLockrejects the hook.
- property pid¶
The PID of the process holding this lock, read from the lock file.
- Returns:
the PID as an integer, or
Noneif the lock file does not exist or cannot be parsed
- property is_lock_held_by_us¶
Whether this lock is held by the current process.
- Returns:
Trueif the lock file exists and names the current process’s PID and hostname
- exception filelock.SoftFileLockLifetimeWarning[source]¶
Bases:
DeprecationWarningThe configured soft-lock lifetime permits overlapping live holders after expiry.
- exception filelock.SoftFileLockProtocolError(lock_file, claim_name, reason)[source]¶
Bases:
OSErrorRaised when strict soft-lock state cannot be interpreted without risking overlap.
- property lock_file¶
The requested lock path.
- property claim_name¶
The claim that caused the error, if scanning identified one.
- property reason¶
The protocol validation failure.
- class filelock.SoftReadWriteLock(lock_file, timeout=-1, *, blocking=True, is_singleton=True, heartbeat_interval=30.0, stale_threshold=None, poll_interval=0.25)[source]¶
Bases:
objectCross-process and cross-host reader/writer lock built on
SoftFileLockprimitives.Use this class instead of
ReadWriteLockwhen the lock file lives on a network filesystem (NFS, Lustre with-o flock, HPC cluster shared storage).ReadWriteLockis backed by SQLite and cannot run on NFS because SQLite’sfcntllocking is unreliable there.Layout on disk for a lock at
foo.lock:foo.lock.state— aSoftFileLocktaken only during state transitions (microseconds).foo.lock.write— writer marker; its presence means a writer is claiming or holding the lock.foo.lock.readers/<host>.<pid>.<uuid>— one file per reader.
Each marker stores a random token (
secrets.token_hex(16)), the holder’s pid, and the holder’s hostname. A daemon heartbeat thread refreshesmtimeon every held marker. A marker whose mtime has not advanced instale_thresholdseconds may be evicted by any process on any host, giving correct behavior when a compute node crashes with a lock held.Writer acquire is two-phase and writer-preferring: phase 1 claims
.write(blocking any new reader), phase 2 waits for existing readers to drain. Writer starvation is impossible.Reentrancy, upgrade/downgrade rules, thread pinning, and singleton caching by resolved path match
ReadWriteLock.Forking invalidates the inherited instance in the child so the child cannot double-own the lock with its parent;
release()on that instance is a no-op, and the child must construct a new instance if it needs a lock.Trust boundary: protects against same-UID non-cooperating processes (one host or cross-host) and same-host different-UID users via
0o600/0o700permissions. Does not protect against root compromise, NTP tampering on same-UID cross-host nodes, or multi-tenant mounts where hostile co-tenants share the UID.- Parameters:
lock_file (
str|PathLike[str]) – path to the lock file; sidecar state/write/readers live next to ittimeout (
float) – maximum wait time in seconds;-1means block indefinitelyblocking (
bool) – ifFalse, raiseTimeoutimmediately on contentionis_singleton (
bool) – ifTrue, reuse existing instances for the same resolved pathheartbeat_interval (
float) – seconds between heartbeat refreshes; default 30 sstale_threshold (
float|None) – seconds ofmtimeinactivity before a marker is stale; defaults to3 * heartbeat_interval, matching etcd’sLeaseKeepAliveconventionpoll_interval (
float) – seconds between acquire retries under contention; default 0.25 s
Added in version 3.27.0.
- read_lock(timeout=None, *, blocking=None)[source]¶
Context manager that acquires and releases a shared read lock.
Falls back to instance defaults for timeout and blocking when
None.- Parameters:
- Raises:
RuntimeError – if a write lock is already held on this instance
Timeout – if the lock cannot be acquired within timeout seconds
- Return type:
- write_lock(timeout=None, *, blocking=None)[source]¶
Context manager that acquires and releases an exclusive write lock.
Falls back to instance defaults for timeout and blocking when
None.- Parameters:
- Raises:
RuntimeError – if a read lock is already held, or a write lock is held by a different thread
Timeout – if the lock cannot be acquired within timeout seconds
- Return type:
- acquire_read(timeout=None, *, blocking=None)[source]¶
Acquire a shared read lock.
If this instance already holds a read lock, the lock level is incremented (reentrant). Attempting to acquire a read lock while holding a write lock raises
RuntimeError(downgrade not allowed). On the 0→1 transition a daemon heartbeat thread is started that refreshes the reader marker’smtimeeveryheartbeat_intervalseconds so peers on other hosts do not evict the marker as stale.- Parameters:
- Return type:
- Returns:
a proxy that can be used as a context manager to release the lock
- Raises:
RuntimeError – if a write lock is already held on this instance, if this instance was invalidated by
os.fork(), or ifclose()was calledTimeout – if the lock cannot be acquired within timeout seconds
- acquire_write(timeout=None, *, blocking=None)[source]¶
Acquire an exclusive write lock.
If this instance already holds a write lock from the same thread, the lock level is incremented (reentrant). Attempting to acquire a write lock while holding a read lock raises
RuntimeError(upgrade not allowed). Write locks are pinned to the acquiring thread: a different thread trying to re-enter also raisesRuntimeError.Writer acquisition runs in two phases. Phase 1 atomically claims
<path>.writeviaO_CREAT | O_EXCL, which immediately blocks any new reader on any host. Phase 2 waits for existing readers to drain. Writer starvation is impossible: new readers see<path>.writeduring phase 2 and wait behind the pending writer.- Parameters:
- Return type:
- Returns:
a proxy that can be used as a context manager to release the lock
- Raises:
RuntimeError – if a read lock is already held, if a write lock is held by a different thread, if this instance was invalidated by
os.fork(), or ifclose()was calledTimeout – if the lock cannot be acquired within timeout seconds
- classmethod get_lock(lock_file, timeout=-1, *, blocking=True)[source]¶
Return the singleton
SoftReadWriteLockfor lock_file.- Parameters:
- Return type:
- Returns:
the singleton lock instance
- Raises:
ValueError – if an instance already exists for this path with different timeout or blocking values
- close()[source]¶
Release any held lock and release internal filesystem resources.
Idempotent. After calling this method the instance can no longer acquire locks — subsequent acquires raise
RuntimeError. A fork-invalidated instance is closed without raising.- Return type:
- release(*, force=False)[source]¶
Release one level of the current lock.
When the lock level reaches zero the heartbeat thread is stopped and the held marker file is unlinked. On a fork-invalidated instance (that is, the child of a
os.fork()call made while the parent held a lock) this method is a no-op so inheritedwithblocks can unwind cleanly in the child.- Parameters:
force (
bool) – ifTrue, release the lock completely regardless of the current lock level- Raises:
RuntimeError – if no lock is currently held and force is
False- Return type:
- class filelock.StrictSoftFileClaim(name, state, token, pid, hostname, start=None)[source]¶
Bases:
objectOne parsed strict soft-lock claim.
- name¶
- state¶
- token¶
- pid¶
- hostname¶
- start = None¶
The owner’s process start token, or
Nonewhen the platform exposes no proven start time. A strict lock never reclaims a claim on its own, so this identifies the owner for tooling rather than driving any automatic break.
- class filelock.StrictSoftFileLock(lock_file, timeout=-1, mode=-1, thread_local=True, *, blocking=True, is_singleton=False, poll_interval=0.05, lifetime=None, context_error_policy='chain', close_error_policy='default', fallback_to_soft=True, preserve_lock_file=False, on_acquired=None)[source]¶
Bases:
BaseFileLockPortable fail-closed lock based on immutable owner claims.
Create a new lock object.
- Parameters:
timeout (
float) – default timeout when acquiring the lock, in seconds. It will be used as fallback value in the acquire method, if no timeout value (None) is given. If you want to disable the timeout, set it to a negative value. A timeout of 0 means that there is exactly one attempt to acquire the file lock.mode (
int) – file permissions for the lockfile. When not specified, the OS controls permissions via umask and default ACLs, preserving POSIX default ACL inheritance in shared directories.thread_local (
bool) – Whether this object’s internal context should be thread local or not. If this is set toFalsethen the lock will be reentrant across threads. WhenTrue(the default), all fields of the lock’s internal context are per-thread, including the configuration valuespoll_interval,timeout,blocking,mode, andlifetime. Setting one of these properties from one thread does not change the value seen by another thread; threads that did not perform the write continue to see the value supplied at construction time.modehas no setter, so construction is the only place it is ever set. If you need configuration values to be visible across threads, construct the lock withthread_local=False.blocking (
bool) – whether the lock should be blocking or notis_singleton (
bool) – If this is set toTruethen only one instance of this class will be created per lock file. This is useful if you want to use the lock object for reentrant locking without needing to pass the same object around.poll_interval (
float) – default interval for polling the lock file, in seconds. It will be used as fallback value in the acquire method, if no poll_interval value (None) is given.lifetime (
float|None) – forSoftFileLock, the age in seconds after which a waiting process may delete the marker, even while its holder remains alive. This legacy expiry mode does not provide strict mutual exclusion.None(the default) disables age-based expiry. Native OS locks (FileLock) cannot be revoked by file age and ignore a non-Nonelifetimewith a warning.context_error_policy (
Literal['chain','group']) – how a context manager reconciles a failure in its body with a failure while releasing on exit."chain"(the default) keeps Python’s behavior: the release error propagates with the body error in its__context__."group"raises aBaseExceptionGroupholding the body error first and the release error second, so neither hides the other.close_error_policy (
Literal['default','raise','suppress']) – what to do with anos.closefailure after relinquishing descriptor ownership."default"keeps each backend’s historical behavior (Unix native locks drop a FUSE/DockerEIO; Windows native locks andSoftFileLockpropagate);"raise"always propagates theOSError;"suppress"always ignores it. Held state is released either way. It does not affect unlock failures or lock-file deletion.fallback_to_soft (
bool) – forUnixFileLock, whether to switch toSoftFileLockwhen the filesystem’sflockreturnsENOSYS.True(the default) keeps the historical fallback;Falsefails closed, letting theENOSYSpropagate so a caller that needs kernel-enforced locking is never silently downgraded. It has no effect on Windows orSoftFileLock.preserve_lock_file (
bool) – for native locks (FileLock), whether filelock promises not to unlink the lock pathname on release.False(the default) keeps each backend’s cleanup: Windows removes the lock file, Unix already leaves it.Truekeeps a stable file identity for ACLs, auditing, and holder metadata: Windows skips its post-release unlink and Unix refuses to enter theENOSYSsoft fallback (which releases by unlinking).SoftFileLockrejectsTrue. The promise covers filelock’s own release path only; it cannot stop another process or the filesystem from removing the pathname.on_acquired (
Callable[[int],None] |None) – for native locks (FileLock), a callable invoked with the borrowed lock descriptor once per physical acquisition, after filelock holds the native lock and finished backend initialization but beforeacquire()returns. Recursive acquisitions do not call it again. The callback may read, write, seek, truncate, or set metadata throughoson the descriptor, but must not close, unlock, or take ownership of it, and filelock does not fsync its writes. If it raises, filelock releases the lock and re-raises.SoftFileLockrejects the hook.
- property claims¶
Published claims that block acquisition.
- exception filelock.Timeout(lock_file)[source]¶
Bases:
TimeoutErrorRaised when the lock could not be acquired in timeout seconds.
- property lock_file¶
The path of the file lock.
- class filelock.UnixFileLock(lock_file, timeout=-1, mode=-1, thread_local=True, *, blocking=True, is_singleton=False, poll_interval=0.05, lifetime=None, context_error_policy='chain', close_error_policy='default', fallback_to_soft=True, preserve_lock_file=False, on_acquired=None)[source]¶
Bases:
BaseFileLockUses the
fcntl.flock()to hard lock the lock file on unix systems.We leave the lock file in place after release. Unlinking a locked file on Unix splits waiters across inodes and breaks mutual exclusion for processes that coordinate via the same path.
Create a new lock object.
- Parameters:
timeout (
float) – default timeout when acquiring the lock, in seconds. It will be used as fallback value in the acquire method, if no timeout value (None) is given. If you want to disable the timeout, set it to a negative value. A timeout of 0 means that there is exactly one attempt to acquire the file lock.mode (
int) – file permissions for the lockfile. When not specified, the OS controls permissions via umask and default ACLs, preserving POSIX default ACL inheritance in shared directories.thread_local (
bool) – Whether this object’s internal context should be thread local or not. If this is set toFalsethen the lock will be reentrant across threads. WhenTrue(the default), all fields of the lock’s internal context are per-thread, including the configuration valuespoll_interval,timeout,blocking,mode, andlifetime. Setting one of these properties from one thread does not change the value seen by another thread; threads that did not perform the write continue to see the value supplied at construction time.modehas no setter, so construction is the only place it is ever set. If you need configuration values to be visible across threads, construct the lock withthread_local=False.blocking (
bool) – whether the lock should be blocking or notis_singleton (
bool) – If this is set toTruethen only one instance of this class will be created per lock file. This is useful if you want to use the lock object for reentrant locking without needing to pass the same object around.poll_interval (
float) – default interval for polling the lock file, in seconds. It will be used as fallback value in the acquire method, if no poll_interval value (None) is given.lifetime (
float|None) – forSoftFileLock, the age in seconds after which a waiting process may delete the marker, even while its holder remains alive. This legacy expiry mode does not provide strict mutual exclusion.None(the default) disables age-based expiry. Native OS locks (FileLock) cannot be revoked by file age and ignore a non-Nonelifetimewith a warning.context_error_policy (
Literal['chain','group']) – how a context manager reconciles a failure in its body with a failure while releasing on exit."chain"(the default) keeps Python’s behavior: the release error propagates with the body error in its__context__."group"raises aBaseExceptionGroupholding the body error first and the release error second, so neither hides the other.close_error_policy (
Literal['default','raise','suppress']) – what to do with anos.closefailure after relinquishing descriptor ownership."default"keeps each backend’s historical behavior (Unix native locks drop a FUSE/DockerEIO; Windows native locks andSoftFileLockpropagate);"raise"always propagates theOSError;"suppress"always ignores it. Held state is released either way. It does not affect unlock failures or lock-file deletion.fallback_to_soft (
bool) – forUnixFileLock, whether to switch toSoftFileLockwhen the filesystem’sflockreturnsENOSYS.True(the default) keeps the historical fallback;Falsefails closed, letting theENOSYSpropagate so a caller that needs kernel-enforced locking is never silently downgraded. It has no effect on Windows orSoftFileLock.preserve_lock_file (
bool) – for native locks (FileLock), whether filelock promises not to unlink the lock pathname on release.False(the default) keeps each backend’s cleanup: Windows removes the lock file, Unix already leaves it.Truekeeps a stable file identity for ACLs, auditing, and holder metadata: Windows skips its post-release unlink and Unix refuses to enter theENOSYSsoft fallback (which releases by unlinking).SoftFileLockrejectsTrue. The promise covers filelock’s own release path only; it cannot stop another process or the filesystem from removing the pathname.on_acquired (
Callable[[int],None] |None) – for native locks (FileLock), a callable invoked with the borrowed lock descriptor once per physical acquisition, after filelock holds the native lock and finished backend initialization but beforeacquire()returns. Recursive acquisitions do not call it again. The callback may read, write, seek, truncate, or set metadata throughoson the descriptor, but must not close, unlock, or take ownership of it, and filelock does not fsync its writes. If it raises, filelock releases the lock and re-raises.SoftFileLockrejects the hook.
- class filelock.WindowsFileLock(lock_file, timeout=-1, mode=-1, thread_local=True, *, blocking=True, is_singleton=False, poll_interval=0.05, lifetime=None, context_error_policy='chain', close_error_policy='default', fallback_to_soft=True, preserve_lock_file=False, on_acquired=None)[source]¶
Bases:
BaseFileLockUses
LockFileExto hard lock a byte range of the lock file on Windows systems.Create a new lock object.
- Parameters:
timeout (
float) – default timeout when acquiring the lock, in seconds. It will be used as fallback value in the acquire method, if no timeout value (None) is given. If you want to disable the timeout, set it to a negative value. A timeout of 0 means that there is exactly one attempt to acquire the file lock.mode (
int) – file permissions for the lockfile. When not specified, the OS controls permissions via umask and default ACLs, preserving POSIX default ACL inheritance in shared directories.thread_local (
bool) – Whether this object’s internal context should be thread local or not. If this is set toFalsethen the lock will be reentrant across threads. WhenTrue(the default), all fields of the lock’s internal context are per-thread, including the configuration valuespoll_interval,timeout,blocking,mode, andlifetime. Setting one of these properties from one thread does not change the value seen by another thread; threads that did not perform the write continue to see the value supplied at construction time.modehas no setter, so construction is the only place it is ever set. If you need configuration values to be visible across threads, construct the lock withthread_local=False.blocking (
bool) – whether the lock should be blocking or notis_singleton (
bool) – If this is set toTruethen only one instance of this class will be created per lock file. This is useful if you want to use the lock object for reentrant locking without needing to pass the same object around.poll_interval (
float) – default interval for polling the lock file, in seconds. It will be used as fallback value in the acquire method, if no poll_interval value (None) is given.lifetime (
float|None) – forSoftFileLock, the age in seconds after which a waiting process may delete the marker, even while its holder remains alive. This legacy expiry mode does not provide strict mutual exclusion.None(the default) disables age-based expiry. Native OS locks (FileLock) cannot be revoked by file age and ignore a non-Nonelifetimewith a warning.context_error_policy (
Literal['chain','group']) – how a context manager reconciles a failure in its body with a failure while releasing on exit."chain"(the default) keeps Python’s behavior: the release error propagates with the body error in its__context__."group"raises aBaseExceptionGroupholding the body error first and the release error second, so neither hides the other.close_error_policy (
Literal['default','raise','suppress']) – what to do with anos.closefailure after relinquishing descriptor ownership."default"keeps each backend’s historical behavior (Unix native locks drop a FUSE/DockerEIO; Windows native locks andSoftFileLockpropagate);"raise"always propagates theOSError;"suppress"always ignores it. Held state is released either way. It does not affect unlock failures or lock-file deletion.fallback_to_soft (
bool) – forUnixFileLock, whether to switch toSoftFileLockwhen the filesystem’sflockreturnsENOSYS.True(the default) keeps the historical fallback;Falsefails closed, letting theENOSYSpropagate so a caller that needs kernel-enforced locking is never silently downgraded. It has no effect on Windows orSoftFileLock.preserve_lock_file (
bool) – for native locks (FileLock), whether filelock promises not to unlink the lock pathname on release.False(the default) keeps each backend’s cleanup: Windows removes the lock file, Unix already leaves it.Truekeeps a stable file identity for ACLs, auditing, and holder metadata: Windows skips its post-release unlink and Unix refuses to enter theENOSYSsoft fallback (which releases by unlinking).SoftFileLockrejectsTrue. The promise covers filelock’s own release path only; it cannot stop another process or the filesystem from removing the pathname.on_acquired (
Callable[[int],None] |None) – for native locks (FileLock), a callable invoked with the borrowed lock descriptor once per physical acquisition, after filelock holds the native lock and finished backend initialization but beforeacquire()returns. Recursive acquisitions do not call it again. The callback may read, write, seek, truncate, or set metadata throughoson the descriptor, but must not close, unlock, or take ownership of it, and filelock does not fsync its writes. If it raises, filelock releases the lock and re-raises.SoftFileLockrejects the hook.
- filelock.lock_descriptor(fd, *, blocking=True, poll_interval=0.05)[source]¶
Take the native OS lock on fd, a file descriptor the caller opened and owns.
This is the same one-byte exclusive lock
FileLockuses, so a descriptor lock and a path lock on the same file contend with each other. UnlikeFileLockit adds no path handling: it never opens, truncates, closes, unlinks, chmods, canonicalizes, or falls back. The caller owns fd before, during, and after the call, and must close it. On Windows fd must be a synchronous descriptor (its handle not opened withFILE_FLAG_OVERLAPPED).For timeout, reentrancy, singleton, lifetime, or stale-break behavior, use
FileLock. There is no async wrapper. Run this in an executor, or driveblocking=Falsefrom your own polling loop.- Parameters:
fd (
int) – an open file descriptor the caller owns.blocking (
bool) – whenTrue(default), retry the nonblocking attempt every poll_interval seconds until it succeeds; whenFalse, make one attempt.poll_interval (
float) – finite, positive seconds between attempts while blocking; ignored when blocking isFalse.
- Return type:
- Returns:
Trueonce the lock is held, orFalseon contention whenblockingisFalse.- Raises:
OSError – for a permanent native failure, such as an invalid descriptor, or with
errno.ENOSYSwhen the Python build lacks the native locking primitive. The descriptor is left open.ValueError – if a blocking call receives a non-finite or non-positive poll_interval.
Added in version 3.30.0.
- filelock.unlock_descriptor(fd)[source]¶
Release the native OS lock on fd without touching the descriptor.
- Parameters:
fd (
int) – the descriptor a priorlock_descriptor()locked; the caller still owns and must close it.- Raises:
OSError – if the native unlock fails, including
errno.ENOSYSwhen the Python build lacks the native locking primitive; the caller may retry on the same descriptor.
Added in version 3.30.0.
- Return type: