@@ -140,9 +140,10 @@ class ControllerWebClientRaw(object):
140
140
_headers = None # Prepared headers for all requests
141
141
_isok = False # Flag to stop
142
142
_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
144
144
_subscriptions : dict [str , Subscription ] # Dictionary that stores the subscriptionId(key) and the corresponding subscription(value)
145
145
_subscriptionLock : threading .Lock # Lock protecting _webSocket and _subscriptions
146
+ _backgroundThread : BackgroundThread = None # The background thread to handle async operations
146
147
147
148
def __init__ (self , baseurl , username , password , locale = None , author = None , userAgent = None , additionalHeaders = None , unixEndpoint = None ):
148
149
self ._baseurl = baseurl
@@ -151,13 +152,9 @@ def __init__(self, baseurl, username, password, locale=None, author=None, userAg
151
152
self ._headers = {}
152
153
self ._isok = True
153
154
154
- self ._webSocket = None
155
155
self ._subscriptions = {}
156
156
self ._subscriptionLock = threading .Lock ()
157
157
158
- # Create the background thread for async operations
159
- self ._backgroundThread = BackgroundThread ()
160
-
161
158
# Create session
162
159
self ._session = requests .Session ()
163
160
@@ -372,6 +369,9 @@ def CallGraphAPI(self, query, variables=None, headers=None, timeout=5.0):
372
369
return content ['data' ]
373
370
374
371
def _EnsureWebSocketConnection (self ):
372
+ if self ._backgroundThread is None :
373
+ # create the background thread for async operations
374
+ self ._backgroundThread = BackgroundThread ()
375
375
if self ._webSocket is None :
376
376
# wait until the connection is established
377
377
self ._backgroundThread .RunCoroutine (self ._OpenWebSocketConnection ()).result ()
0 commit comments