diff --git a/mars/oscar/backends/router.py b/mars/oscar/backends/router.py index 9eecaffb93..5b37420d19 100644 --- a/mars/oscar/backends/router.py +++ b/mars/oscar/backends/router.py @@ -30,7 +30,6 @@ class Router: "_mapping", "_comm_config", "_cache_local", - "_lock", ) _instance: "Router" = None @@ -64,7 +63,6 @@ def __init__( self._mapping = mapping self._comm_config = comm_config or dict() self._cache_local = threading.local() - self._lock = asyncio.Lock() @property def _cache(self) -> Dict[Tuple[str, Any, Optional[Type[Client]]], Client]: @@ -74,6 +72,14 @@ def _cache(self) -> Dict[Tuple[str, Any, Optional[Type[Client]]], Client]: cache = self._cache_local.cache = dict() return cache + @property + def _lock(self) -> asyncio.Lock: + try: + return self._cache_local.lock + except AttributeError: + lock = self._cache_local.lock = asyncio.Lock() + return lock + def set_mapping(self, mapping: Dict[str, str]): self._mapping = mapping self._cache_local = threading.local()