Skip to content

Commit

Permalink
Async clusters: Support creating locks inside async functions (#2471)
Browse files Browse the repository at this point in the history
Co-authored-by: Chayim <chayim@users.noreply.github.com>
  • Loading branch information
utkarshgupta137 and chayim committed Dec 4, 2022
1 parent c48dc83 commit a114f26
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion redis/asyncio/cluster.py
Expand Up @@ -356,11 +356,13 @@ def __init__(
)

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:
Expand All @@ -378,6 +380,8 @@ async def initialize(self) -> "RedisCluster":
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
Expand Down

0 comments on commit a114f26

Please sign in to comment.