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

Confusing but perhaps deliberate behaviour ? #402

Open
znichollscr opened this issue Feb 24, 2025 · 2 comments
Open

Confusing but perhaps deliberate behaviour ? #402

znichollscr opened this issue Feb 24, 2025 · 2 comments

Comments

@znichollscr
Copy link

Hi there, thanks for the package. A perhaps naive question, but the following behaviour seemed unintuitive to me. In short, if you chain method calls and don't assign the result to a variable, you don't acquire the lock. However, if you do assign the result, even to _, you acquire the lock. I must be missing something, but I can't tell what.

A minimum reproducible example is below.

import tempfile
from pathlib import Path

import filelock

lock_file = Path(tempfile.mkdtemp()) / "index.json.lock"

# For some reason, this doesn't cause the lock to actually be acquired.
filelock.SoftFileLock(lock_file).acquire()
print(f"{lock_file.exists()=}")

# But this does
_ = filelock.SoftFileLock(lock_file).acquire()
print(f"{lock_file.exists()=}")

For what it's worth, I suspect the same underlying confusion causes this and #400. (#400 seems to also be a confusion about the flow, because the acquire method does take polling_interval as an argument, but it is true that __enter__ does not. Maybe the answer is, "Always use acquire as a context manager, never antying else". If that is the answer, all fine, but it might be worth clarifying and updating the docs as current examples suggeset that using acquire not as a context manager is an expected use case too.)

@znichollscr
Copy link
Author

I've just seen that __del__ calls release. This appears deliberate, but it does seem quite confusing. If you'd welcome it, I'd happily add a section on this to the docs.

@gaborbernat
Copy link
Member

PR welcome 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants