Skip to content

Commit bbbb6d8

Browse files
author
Barkin Simsek
committed
Launch the background thread only when needed.
1 parent 0029e36 commit bbbb6d8

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

python/mujinwebstackclient/controllerwebclientraw.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,10 @@ class ControllerWebClientRaw(object):
140140
_headers = None # Prepared headers for all requests
141141
_isok = False # Flag to stop
142142
_session = None # Requests session object
143-
_webSocket: websockets.asyncio.client.ClientConnection # WebSocket used to connect to WebStack for subscriptions
143+
_webSocket: websockets.asyncio.client.ClientConnection = None # WebSocket used to connect to WebStack for subscriptions
144144
_subscriptions: dict[str, Subscription] # Dictionary that stores the subscriptionId(key) and the corresponding subscription(value)
145145
_subscriptionLock: threading.Lock # Lock protecting _webSocket and _subscriptions
146+
_backgroundThread: BackgroundThread = None # The background thread to handle async operations
146147

147148
def __init__(self, baseurl, username, password, locale=None, author=None, userAgent=None, additionalHeaders=None, unixEndpoint=None):
148149
self._baseurl = baseurl
@@ -151,13 +152,9 @@ def __init__(self, baseurl, username, password, locale=None, author=None, userAg
151152
self._headers = {}
152153
self._isok = True
153154

154-
self._webSocket = None
155155
self._subscriptions = {}
156156
self._subscriptionLock = threading.Lock()
157157

158-
# Create the background thread for async operations
159-
self._backgroundThread = BackgroundThread()
160-
161158
# Create session
162159
self._session = requests.Session()
163160

@@ -372,6 +369,9 @@ def CallGraphAPI(self, query, variables=None, headers=None, timeout=5.0):
372369
return content['data']
373370

374371
def _EnsureWebSocketConnection(self):
372+
if self._backgroundThread is None:
373+
# create the background thread for async operations
374+
self._backgroundThread = BackgroundThread()
375375
if self._webSocket is None:
376376
# wait until the connection is established
377377
self._backgroundThread.RunCoroutine(self._OpenWebSocketConnection()).result()

0 commit comments

Comments
 (0)