diff options
author | Utkarsh Gupta <utkarshgupta137@gmail.com> | 2022-12-04 13:34:17 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-04 10:04:17 +0200 |
commit | a114f26c03aa9127826cf39c60c759340f93653b (patch) | |
tree | 0474ad4bbe610f30515335ce79719f492bad0717 | |
parent | c48dc8310717344374db6a31000e92cfe1ae35f8 (diff) | |
download | redis-py-a114f26c03aa9127826cf39c60c759340f93653b.tar.gz |
Async clusters: Support creating locks inside async functions (#2471)
Co-authored-by: Chayim <chayim@users.noreply.github.com>
-rw-r--r-- | redis/asyncio/cluster.py | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/redis/asyncio/cluster.py b/redis/asyncio/cluster.py index ac61314..5a2dffd 100644 --- a/redis/asyncio/cluster.py +++ b/redis/asyncio/cluster.py @@ -356,11 +356,13 @@ class RedisCluster(AbstractRedis, AbstractRedisCluster, AsyncRedisClusterCommand ) self._initialize = True - self._lock = asyncio.Lock() + self._lock: Optional[asyncio.Lock] = None async def initialize(self) -> "RedisCluster": """Get all nodes from startup nodes & creates connections if not initialized.""" if self._initialize: + if not self._lock: + self._lock = asyncio.Lock() async with self._lock: if self._initialize: try: @@ -378,6 +380,8 @@ class RedisCluster(AbstractRedis, AbstractRedisCluster, AsyncRedisClusterCommand async def close(self) -> None: """Close all connections & client if initialized.""" if not self._initialize: + if not self._lock: + self._lock = asyncio.Lock() async with self._lock: if not self._initialize: self._initialize = True |