This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 335
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use ContextVar to track Task-local Lock token
The Lock implementation in redis-py uses thread-local storage so that multiple threads using the same Lock instance can acquire the Lock from each other. Thread-local storage ensures that each thread sees a different token value. Thread-local storage does not apply in the Task-based concurrency that asyncio programs use. To achieve a similar effect, we need to embed a ContextVar instance within each Lock and store the Lock instance's token withint he ContextVar instance. This allows every Task that uses the same Lock instance to see a different token. Thus, if both Task A and Task B refer to Lock 1, Task A can "acquire" Lock 1 and block Task B from acquiring the same Lock until Task A "releases" the Lock. NOTE: The Python documentation suggests only storing ContextVar instances in the top-level module scope due to issues around garbage collection. That won't work in the current design of Lock. For lack of a better alternative, and to preserve the original design of Lock taken from redis-py, we have created instances of ContextVar within instances of Lock. Fixes #1040.
- Loading branch information
Andrew Brookins
committed
Jul 29, 2021
1 parent
b2952d9
commit 028da4f
Showing
3 changed files
with
91 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters