Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read/Write File Lock #380

Closed
wants to merge 43 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
bb25b9b
WIP
Yard1 Nov 23, 2024
fe38354
Docstrings
Yard1 Nov 23, 2024
e0e4911
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 23, 2024
c669bdc
Fix inner/outer for write
Yard1 Nov 23, 2024
90a38bf
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 23, 2024
75d7456
Lint
Yard1 Nov 23, 2024
3442e6b
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 23, 2024
efd8de1
Lint
Yard1 Nov 23, 2024
b31ce04
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 23, 2024
8a84b54
Lint
Yard1 Nov 23, 2024
a0cba46
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 23, 2024
9a3b7cb
Nit
Yard1 Nov 23, 2024
bad2bd1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 23, 2024
8661be0
Tweak
Yard1 Nov 23, 2024
eed9492
Update _wrapper.py
Yard1 Nov 23, 2024
3ab83c3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 23, 2024
67b53eb
Fixes
Yard1 Nov 23, 2024
c7f8488
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 23, 2024
b5268f9
Fixes
Yard1 Nov 23, 2024
1787fb7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 23, 2024
02f7dd7
Lint
Yard1 Nov 23, 2024
978b128
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 23, 2024
a0c9e9a
Lint
Yard1 Nov 23, 2024
55b2020
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Nov 23, 2024
1dd52b1
Fix
Yard1 Nov 28, 2024
a6d65c6
Asyncio
Yard1 Dec 1, 2024
c12b68b
Remove thread-local
Yard1 Dec 1, 2024
46fba64
Tests, fixes
Yard1 Dec 1, 2024
ab6f64d
Lint
Yard1 Dec 2, 2024
2d08ebc
Docs
Yard1 Dec 2, 2024
eac9062
Docs
Yard1 Dec 2, 2024
da41d44
Fix
Yard1 Dec 2, 2024
e9be639
Fix
Yard1 Dec 2, 2024
321a524
Example
Yard1 Dec 2, 2024
34b209a
Use protocol
Yard1 Dec 3, 2024
d1196f6
Protocols
Yard1 Dec 3, 2024
9caea5e
Default poll interval constant
Yard1 Dec 3, 2024
6dd9736
Remove extra backtics
Yard1 Dec 3, 2024
ec5f49c
Merge branch 'main' into read_write_lock
gaborbernat Jan 3, 2025
d9afe63
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 3, 2025
1afe716
Update _api.py
Yard1 Jan 12, 2025
913f496
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 12, 2025
1c0680b
Update _api.py
Yard1 Jan 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Remove thread-local
  • Loading branch information
Yard1 committed Dec 1, 2024
commit c12b68b53d6ff74e732e4a89c6156c16210c7539
11 changes: 5 additions & 6 deletions src/filelock/read_write/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def __init__( # noqa: PLR0913
lock_file: str | os.PathLike[str] | None = None,
timeout: float = -1,
mode: int = 0o644,
thread_local: bool = True, # noqa: FBT001, FBT002
*,
blocking: bool = True,
lock_file_inner: str | os.PathLike[str] | None = None,
Expand All @@ -50,6 +49,8 @@ def __init__( # noqa: PLR0913

This object will use two lock files to ensure writers have priority over readers.

Note that this lock is always thread-local, to allow for non-exclusive access.

:param read_write_mode: whether this object should be in WRITE mode or READ mode.
:param lock_file: path to the file. Note that two files will be created: \
``{lock_file}.inner`` and ``{lock_file}.outer``. \
Expand All @@ -58,8 +59,6 @@ def __init__( # noqa: PLR0913
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.
:param mode: file permissions for the lockfile
:param thread_local: Whether this object's internal context should be thread local or not. If this is set to \
``False`` then the lock will be reentrant across threads.
:param blocking: whether the lock should be blocking or not
:param lock_file_inner: path to the inner lock file. Can be left unspecified if ``lock_file`` is specified.
:param lock_file_outer: path to the outer lock file Can be left unspecified if ``lock_file`` is specified.
Expand Down Expand Up @@ -87,15 +86,15 @@ def __init__( # noqa: PLR0913
lock_file_inner,
timeout=timeout,
mode=mode,
thread_local=thread_local,
thread_local=True,
blocking=blocking,
is_singleton=False,
)
self._outer_lock = file_lock_cls(
lock_file_outer,
timeout=timeout,
mode=mode,
thread_local=thread_local,
thread_local=True,
blocking=blocking,
is_singleton=False,
)
Expand Down Expand Up @@ -165,7 +164,7 @@ def is_locked(self) -> bool:
@property
def lock_counter(self) -> int:
""":return: The number of times this lock has been acquired (but not yet released)."""
return self._inner_lock.lock_counter + self._inner_lock_shared.lock_counter
return self._inner_lock.lock_counter + self._outer_lock.lock_counter

def acquire(
self,
Expand Down
3 changes: 0 additions & 3 deletions src/filelock/read_write/_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def __init__( # noqa: PLR0913
lock_file: str | os.PathLike[str] | None = None,
timeout: float = -1,
mode: int = 0o644,
thread_local: bool = True, # noqa: FBT001, FBT002
*,
blocking: bool = True,
lock_file_inner: str | os.PathLike[str] | None = None,
Expand All @@ -35,7 +34,6 @@ def __init__( # noqa: PLR0913
read_write_mode=ReadWriteMode.READ,
timeout=timeout,
mode=mode,
thread_local=thread_local,
blocking=blocking,
)
self.write_lock = self._read_write_file_lock_cls(
Expand All @@ -45,7 +43,6 @@ def __init__( # noqa: PLR0913
read_write_mode=ReadWriteMode.WRITE,
timeout=timeout,
mode=mode,
thread_local=thread_local,
blocking=blocking,
)

Expand Down
13 changes: 6 additions & 7 deletions src/filelock/read_write/asyncio/_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def __init__( # noqa: PLR0913
timeout: float = -1,
mode: int = 0o644,
*,
thread_local: bool = True,
blocking: bool = True,
lock_file_inner: str | os.PathLike[str] | None = None,
lock_file_outer: str | os.PathLike[str] | None = None,
Expand All @@ -48,6 +47,8 @@ def __init__( # noqa: PLR0913

This object will use two lock files to ensure writers have priority over readers.

Note that this lock is always thread-local, to allow for non-exclusive access.

:param read_write_mode: whether this object should be in WRITE mode or READ mode.
:param lock_file: path to the file. Note that two files will be created: \
``{lock_file}.inner`` and ``{lock_file}.outer``. \
Expand All @@ -56,8 +57,6 @@ def __init__( # noqa: PLR0913
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.
:param mode: file permissions for the lockfile
:param thread_local: Whether this object's internal context should be thread local or not. If this is set to \
``False`` then the lock will be reentrant across threads.
:param blocking: whether the lock should be blocking or not
:param lock_file_inner: path to the inner lock file. Can be left unspecified if ``lock_file`` is specified.
:param lock_file_outer: path to the outer lock file Can be left unspecified if ``lock_file`` is specified.
Expand All @@ -67,7 +66,7 @@ def __init__( # noqa: PLR0913
lock_file=lock_file,
timeout=timeout,
mode=mode,
thread_local=thread_local,
thread_local=True,
blocking=blocking,
lock_file_inner=lock_file_inner,
lock_file_outer=lock_file_outer,
Expand All @@ -78,7 +77,7 @@ def __init__( # noqa: PLR0913
self.lock_file_inner,
timeout=self._inner_lock.timeout,
mode=self._inner_lock.mode,
thread_local=self._inner_lock.is_thread_local(),
thread_local=True,
blocking=self._inner_lock.blocking,
is_singleton=False,
)
Expand All @@ -87,7 +86,7 @@ def __init__( # noqa: PLR0913
self.lock_file_inner,
timeout=self._inner_lock.timeout,
mode=self._inner_lock.mode,
thread_local=self._inner_lock.is_thread_local(),
thread_local=True,
blocking=self._inner_lock.blocking,
is_singleton=False,
)
Expand All @@ -97,7 +96,7 @@ def __init__( # noqa: PLR0913
self.lock_file_outer,
timeout=self._outer_lock.timeout,
mode=self._outer_lock.mode,
thread_local=self._outer_lock.is_thread_local(),
thread_local=True,
blocking=self._outer_lock.blocking,
is_singleton=False,
)
Expand Down
3 changes: 0 additions & 3 deletions src/filelock/read_write/asyncio/_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def __init__( # noqa: PLR0913
lock_file: str | os.PathLike[str] | None = None,
timeout: float = -1,
mode: int = 0o644,
thread_local: bool = True, # noqa: FBT001, FBT002
*,
blocking: bool = True,
lock_file_inner: str | os.PathLike[str] | None = None,
Expand All @@ -43,7 +42,6 @@ def __init__( # noqa: PLR0913
read_write_mode=ReadWriteMode.READ,
timeout=timeout,
mode=mode,
thread_local=thread_local,
blocking=blocking,
loop=loop,
run_in_executor=run_in_executor,
Expand All @@ -56,7 +54,6 @@ def __init__( # noqa: PLR0913
read_write_mode=ReadWriteMode.WRITE,
timeout=timeout,
mode=mode,
thread_local=thread_local,
blocking=blocking,
loop=loop,
run_in_executor=run_in_executor,
Expand Down