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. Eachasynciotask owns its own hold. Tasks share a read lock, and a task asking for the write lock waits until the other holders release, with waiting writers ahead of new readers. As withReadWriteLock, upgrading or downgrading a held lock raisesRuntimeError. Singleton wrappers over one path share their task holds.- 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=None, *, blocking=None)[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 the calling task already holds the write lock
Timeout – if the lock cannot be acquired within timeout seconds
- async acquire_write(timeout=None, *, blocking=None)[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 the calling task already holds the read lock
Timeout – if the lock cannot be acquired within timeout seconds
- async release(*, force=False)[source]¶
Release one level of the calling task’s hold.
The wrapper releases the database lock once no task holds it.
- Parameters:
force (
bool) – ifTrue, drop the calling task’s whole hold at any nesting level- Raises:
RuntimeError – if the calling task holds no lock 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, on_compromise=None, 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(). Eachasynciotask owns its own hold. Tasks share a read lock, and a task asking for the write lock waits until the other holders release, with waiting writers ahead of new readers. Upgrading or downgrading a held lock raisesRuntimeError. The underlyingSoftReadWriteLockhandles forks, the heartbeat, and stale eviction. Singleton wrappers share one sync lock and its task holds.- Parameters:
lock_file (
str|PathLike[str]) – path to the lock file; the protocol directory lives next to it as<lock_file>.rwtimeout (
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 a holder record may stay unchanged before a contender evicts it; defaults to3 * heartbeat_intervalpoll_interval (
float) – seconds between acquire retries under contention; default 0.25 son_compromise (
Callable[[LeaseCompromise],None] |None) – called from the heartbeat thread with aLeaseCompromisewhen the hold is lostloop (
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 generation¶
The generation the current hold was granted at, a fencing token;
Nonewhen no lock is held.
- property compromise¶
How the current hold was lost, or
Nonewhile it stands.
- 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 the calling task already holds the write lock
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 the calling task already holds the read lock
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 the calling task already holds the write lock, 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 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 the calling task already holds the read lock, 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 calling task’s hold.
The wrapper releases the lock once no task holds it.
- Parameters:
force (
bool) – ifTrue, drop the calling task’s whole hold at any nesting level- Raises:
RuntimeError – if the calling task holds no lock 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) – whether to retry until this call holds the lock or the timeout expires,Nonemeans use the defaultblocking.Falsemakes one attempt and raisesTimeouton failure.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. Construction raisesValueErrorunless it grants the owner read and write, which the lock needs for its own files.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) – whether to retry until this call holds the lock or the timeout expires,Nonemeans use the defaultblocking.Falsemakes one attempt and raisesTimeouton failure.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. Construction raisesValueErrorunless it grants the owner read and write, which the lock needs for its own files.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:
NamedTupleKeep optional metadata when reading unknown lock modes.
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=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).- 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=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.- 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, or if a write lock is held by another thread and force isFalse- 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. Construction raisesValueErrorunless it grants the owner read and write, which the lock needs for its own files.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, on_compromise=None)[source]¶
Bases:
objectCross-process and cross-host reader/writer lock for shared filesystems.
Use this class instead of
ReadWriteLockwhen the lock file lives on a network filesystem (NFS, Lustre, HPC cluster shared storage).ReadWriteLockis backed by SQLite and cannot run on NFS because SQLite’sfcntllocking is unreliable there.The lock’s state is a log of immutable snapshots under
foo.lock.rw/gen/<N>, each naming the writer and the readers holding the lock at generationN. Every acquire, release, and eviction publishes the next snapshot with one atomic no-replace hard link, so no transition can be left half done by a crash on any host. Each participant also keepsfoo.lock.rw/holders/<token>, a record whose nonce a daemon heartbeat thread rewrites everyheartbeat_intervalseconds. A contender evicts a member whose record has not changed forstale_thresholdseconds of the contender’s own monotonic clock; no clock is ever compared across hosts, so skew between hosts or against the file server cannot make a live holder look dead.Writer acquire is writer-preferring: a writer enters the snapshot as soon as no live writer is named, which blocks any new reader, then waits for the named readers to leave. Writer starvation is impossible.
An expired holder is not fenced by the lock: a process paused past
stale_thresholdresumes believing it holds the lock until its next heartbeat reports the loss throughon_compromise.generationis a monotonic fencing token for the protected resource: a store that rejects writes carrying a lower generation than the highest it has accepted refuses such a holder.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 or multi-tenant mounts where hostile co-tenants share the UID.- Parameters:
lock_file (
str|PathLike[str]) – path to the lock file; the protocol directory lives next to it as<lock_file>.rwtimeout (
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 a holder record may stay unchanged before a contender evicts it; defaults to3 * heartbeat_interval, matching etcd’sLeaseKeepAliveconventionpoll_interval (
float) – seconds between acquire retries under contention; default 0.25 son_compromise (
Callable[[LeaseCompromise],None] |None) – called from the heartbeat thread with aLeaseCompromisewhen the hold is lost: a peer evicted it, or refreshes failed for long enough that a peer could have
Added in version 3.27.0.
Changed in version 3.33.0: The on-disk protocol is a generation log; earlier releases cannot share a lock path with this one.
- property generation¶
The generation at which the current hold was granted, or
Nonewhen no lock is held.Generations are monotonic across all participants and hosts, so this is a fencing token: pass it to the protected resource and have the resource reject any operation carrying a lower generation than the highest it has accepted. That refuses a holder that paused past
stale_threshold, was evicted, and resumed.Added in version 3.33.0.
- property compromise¶
How the current hold was lost, or
Nonewhile it stands.Set by the heartbeat thread once a peer has evicted this holder or refreshes have failed for long enough that a peer could have. The holder should stop using the protected resource when it is set.
Added in version 3.33.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 the reader publishes its holder record, enters the next snapshot once no live writer is named, and starts a daemon heartbeat thread that rewrites the record’s nonce everyheartbeat_intervalseconds so peers on other hosts do not evict it.- 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
SoftFileLockProtocolError – if a snapshot cannot be read without risking overlap, or the filesystem refuses the no-replace hard links the protocol commits with
- 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.A writer enters the next snapshot as its writer as soon as no live writer is named, which blocks every new reader on every host, then waits for the readers that snapshot names to leave. Writer starvation is impossible: new readers see the named writer and wait behind it.
- 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
SoftFileLockProtocolError – if a snapshot cannot be read without risking overlap, or the filesystem refuses the no-replace hard links the protocol commits with
- 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 mark the instance closed.
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, the participant is committed out of the next snapshot, and its holder record is removed. 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, or if a write lock is held by another thread and force isFalse- 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. Construction raisesValueErrorunless it grants the owner read and write, which the lock needs for its own files.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. Construction raisesValueErrorunless it grants the owner read and write, which the lock needs for its own files.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. Construction raisesValueErrorunless it grants the owner read and write, which the lock needs for its own files.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: