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

Locking check interval #334

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

nickderobertis
Copy link
Contributor

Adds a check_interval option to the @cache.locked decorator, which controls how long the backend will wait in between attempts to set the lock while another task has acquired the lock.

This helps avoid hitting the backend many times in a short timespan when concurrent async tasks or processes are trying to run the same locked function.

The default behavior of the lib is unchanged as the default value of check_interval=0, so it still does await asyncio.sleep(0) to just proceed to the next task on the event loop without wait when the argument is not specified.

More context in #333. This gives a "good enough" solution for my use case for now but we may consider using a pub/sub approach in the future for backends that could support it (Redis).

Also a couple small test updates (let me know if you want me to separate these out):

  1. Clear the backend before every test. I noticed that tests could affect each other while I was hacking on them
  2. Add protected=False to the @cache(lock=True) test as when protected=True the test passes without even passing lock=True.

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

Successfully merging this pull request may close these issues.

1 participant