Skip to content

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.

@Krukov Krukov merged commit 24577ad into Krukov:master Mar 9, 2025
1 of 6 checks passed
@Krukov
Copy link
Owner

Krukov commented Mar 9, 2025

Thanks for your contribution and sorry I haven't looked here for a long time

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.

2 participants