Skip to content

Commit

Permalink
async_cluster: create asyncio.Lock inside async functions
Browse files Browse the repository at this point in the history
This allows creating the client at the module level or before the event loop has been initialized.
  • Loading branch information
utkarshgupta137 committed Nov 24, 2022
1 parent 54a1dce commit ca50834
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion redis/asyncio/cluster.py
Expand Up @@ -315,11 +315,13 @@ def __init__(
list(res.values())[0], **kwargs
)
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 @@ -337,6 +339,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 ca50834

Please sign in to comment.