Skip to content

Commit 735164d

Browse files
authored
Refactor locking mechanism to use asyncio.Lock for asynchronous container initialization (#185)
1 parent 5c8ee5d commit 735164d

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

libraries/microsoft-agents-storage-cosmos/microsoft_agents/storage/cosmos/cosmos_db_storage.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Licensed under the MIT License.
33

44
from typing import TypeVar, Union
5-
from threading import Lock
5+
import asyncio
66

77
from azure.cosmos import (
88
documents,
@@ -49,7 +49,7 @@ def __init__(self, config: CosmosDBStorageConfig):
4949
self._container: ContainerProxy = None
5050
self._compatability_mode_partition_key: bool = False
5151
# Lock used for synchronizing container creation
52-
self._lock: Lock = Lock()
52+
self._lock: asyncio.Lock = asyncio.Lock()
5353

5454
def _create_client(self) -> CosmosClient:
5555
if self._config.url:
@@ -164,8 +164,8 @@ async def _create_container(self) -> None:
164164

165165
async def initialize(self) -> None:
166166
if not self._container:
167-
with self._lock:
168-
# in case another thread attempted to initialize just before acquiring the lock
167+
async with self._lock:
168+
# in case another async task attempted to initialize just before acquiring the lock
169169
if self._container:
170170
return
171171

0 commit comments

Comments
 (0)