Skip to content

Commit fb1a50f

Browse files
committed
Lazy load the executor
1 parent 51b9228 commit fb1a50f

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

databricks/sdk/oauth.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,19 @@ class _TokenState(Enum):
205205
class Refreshable(TokenSource):
206206
"""A token source that supports refreshing expired tokens."""
207207

208-
_EXECUTOR = ThreadPoolExecutor(max_workers=10)
208+
_EXECUTOR = None
209+
_EXECUTOR_LOCK = threading.Lock()
209210
_DEFAULT_STALE_DURATION = timedelta(minutes=3)
210211

212+
@classmethod
213+
def _get_executor(cls):
214+
"""Lazy initialization of the ThreadPoolExecutor."""
215+
if cls._EXECUTOR is None:
216+
with cls._EXECUTOR_LOCK:
217+
if cls._EXECUTOR is None:
218+
cls._EXECUTOR = ThreadPoolExecutor(max_workers=10)
219+
return cls._EXECUTOR
220+
211221
def __init__(self,
212222
token: Token = None,
213223
disable_async: bool = True,
@@ -289,7 +299,7 @@ def _refresh_internal():
289299
with self._lock:
290300
if not self._is_refreshing and not self._refresh_err:
291301
self._is_refreshing = True
292-
self._EXECUTOR.submit(_refresh_internal)
302+
Refreshable._get_executor().submit(_refresh_internal)
293303

294304
@abstractmethod
295305
def refresh(self) -> Token:

0 commit comments

Comments
 (0)