MCPcopy Create free account
hub / github.com/tox-dev/filelock / __init__

Method __init__

src/filelock/_api.py:647–735  ·  view source on GitHub ↗

Create a new lock object. :param lock_file: path to the file :param timeout: 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

(  # ruff:ignore[too-many-arguments]  # public constructor: one parameter per documented lock option
        self,
        lock_file: str | os.PathLike[str],
        timeout: float = -1,
        mode: int = _UNSET_FILE_MODE,
        thread_local: bool = True,  # ruff:ignore[boolean-type-hint-positional-argument, boolean-default-value-positional-argument]  # public API: positional bool kept for backwards compatibility
        *,
        blocking: bool = True,
        is_singleton: bool = False,
        poll_interval: float = 0.05,
        lifetime: float | None = None,
        context_error_policy: ContextErrorPolicy = "chain",
        close_error_policy: CloseErrorPolicy = "default",
        fallback_to_soft: bool = True,
        preserve_lock_file: bool = False,
        on_acquired: Callable[[int], None] | None = None,
    )

Source from the content-addressed store, hash-verified

645 cls._instances_under_construction = set()
646
647 def __init__( # ruff:ignore[too-many-arguments] # public constructor: one parameter per documented lock option
648 self,
649 lock_file: str | os.PathLike[str],
650 timeout: float = -1,
651 mode: int = _UNSET_FILE_MODE,
652 thread_local: bool = True, # ruff:ignore[boolean-type-hint-positional-argument, boolean-default-value-positional-argument] # public API: positional bool kept for backwards compatibility
653 *,
654 blocking: bool = True,
655 is_singleton: bool = False,
656 poll_interval: float = 0.05,
657 lifetime: float | None = None,
658 context_error_policy: ContextErrorPolicy = "chain",
659 close_error_policy: CloseErrorPolicy = "default",
660 fallback_to_soft: bool = True,
661 preserve_lock_file: bool = False,
662 on_acquired: Callable[[int], None] | None = None,
663 ) -> None:
664 """
665 Create a new lock object.
666
667 :param lock_file: path to the file
668 :param timeout: default timeout when acquiring the lock, in seconds. It will be used as fallback value in the
669 acquire method, if no timeout value (``None``) is given. If you want to disable the timeout, set it to a
670 negative value. A timeout of 0 means that there is exactly one attempt to acquire the file lock.
671 :param mode: file permissions for the lockfile. When not specified, the OS controls permissions via umask and
672 default ACLs, preserving POSIX default ACL inheritance in shared directories.
673 :param thread_local: Whether this object's internal context should be thread local or not. If this is set to
674 ``False`` then the lock will be reentrant across threads. When ``True`` (the default), **all fields of the
675 lock's internal context are per-thread**, including the configuration values ``poll_interval``, ``timeout``,
676 ``blocking``, ``mode``, and ``lifetime``. Setting one of these properties from one thread does not change
677 the value seen by another thread; threads that did not perform the write continue to see the value supplied
678 at construction time. If you need configuration values to be visible across threads, construct the lock
679 with ``thread_local=False``.
680 :param blocking: whether the lock should be blocking or not
681 :param is_singleton: If this is set to ``True`` then only one instance of this class will be created per lock
682 file. This is useful if you want to use the lock object for reentrant locking without needing to pass the
683 same object around.
684 :param poll_interval: default interval for polling the lock file, in seconds. It will be used as fallback value
685 in the acquire method, if no poll_interval value (``None``) is given.
686 :param lifetime: for :class:`SoftFileLock`, the age in seconds after which a waiting process may delete the
687 marker, even while its holder remains alive. This legacy expiry mode does not provide strict mutual
688 exclusion. ``None`` (the default) disables age-based expiry. Native OS locks (:class:`FileLock`) cannot be
689 revoked by file age and ignore a non-``None`` ``lifetime`` with a warning.
690 :param context_error_policy: how a context manager reconciles a failure in its body with a failure while
691 releasing on exit. ``"chain"`` (the default) keeps Python's behavior: the release error propagates with the
692 body error in its ``__context__``. ``"group"`` raises a :class:`BaseExceptionGroup` holding the body error
693 first and the release error second, so neither hides the other.
694 :param close_error_policy: what to do with an ``os.close`` failure after relinquishing descriptor ownership.
695 ``"default"`` keeps each backend's historical behavior (Unix native locks drop a FUSE/Docker ``EIO``;
696 Windows native locks and :class:`SoftFileLock` propagate); ``"raise"`` always propagates the ``OSError``;
697 ``"suppress"`` always ignores it. Held state is released either way. It does not affect unlock failures or
698 lock-file deletion.
699 :param fallback_to_soft: for :class:`UnixFileLock`, whether to switch to :class:`SoftFileLock` when the
700 filesystem's ``flock`` returns ``ENOSYS``. ``True`` (the default) keeps the historical fallback;
701 ``False`` fails closed, letting the ``ENOSYS`` propagate so a caller that needs kernel-enforced
702 locking is never silently downgraded. It has no effect on Windows or :class:`SoftFileLock`.
703 :param preserve_lock_file: for native locks (:class:`FileLock`), whether filelock promises not to unlink the
704 lock pathname on release. ``False`` (the default) keeps each backend's cleanup: Windows removes the lock

Callers

nothing calls this directly

Calls 1

_register_fork_objectFunction · 0.85

Tested by

no test coverage detected