From 1b32bbb99901cbc4498014590d924f53b5e448d7 Mon Sep 17 00:00:00 2001 From: Utkarsh Gupta Date: Thu, 24 Nov 2022 13:02:57 +0530 Subject: [PATCH] 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. --- redis/asyncio/cluster.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/redis/asyncio/cluster.py b/redis/asyncio/cluster.py index d5a38b2878..184e271695 100644 --- a/redis/asyncio/cluster.py +++ b/redis/asyncio/cluster.py @@ -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: @@ -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