Skip to content

Commit ca50834

Browse files
async_cluster: create asyncio.Lock inside async functions
This allows creating the client at the module level or before the event loop has been initialized.
1 parent 54a1dce commit ca50834

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

redis/asyncio/cluster.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,13 @@ def __init__(
315315
list(res.values())[0], **kwargs
316316
)
317317
self._initialize = True
318-
self._lock = asyncio.Lock()
318+
self._lock: Optional[asyncio.Lock] = None
319319

320320
async def initialize(self) -> "RedisCluster":
321321
"""Get all nodes from startup nodes & creates connections if not initialized."""
322322
if self._initialize:
323+
if not self._lock:
324+
self._lock = asyncio.Lock()
323325
async with self._lock:
324326
if self._initialize:
325327
try:
@@ -337,6 +339,8 @@ async def initialize(self) -> "RedisCluster":
337339
async def close(self) -> None:
338340
"""Close all connections & client if initialized."""
339341
if not self._initialize:
342+
if not self._lock:
343+
self._lock = asyncio.Lock()
340344
async with self._lock:
341345
if not self._initialize:
342346
self._initialize = True

0 commit comments

Comments
 (0)