Skip to content

Commit 404fe44

Browse files
authored
Merge pull request unitreerobotics#89 from silencht/master
feature: add thread-safe lazy initialization to ChannelFactory users: This is a test feature. If problems occur, please revert the code.
2 parents f559291 + 59b1c35 commit 404fe44

1 file changed

Lines changed: 40 additions & 29 deletions

File tree

unitree_sdk2py/core/channel.py

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import time
22
from typing import Any, Callable
3+
import threading
34
from threading import Thread, Event
45

56
from cyclonedds.domain import Domain, DomainParticipant
@@ -192,41 +193,51 @@ class ChannelFactory(Singleton):
192193
__participant = None
193194
__qos = None
194195

196+
__initialized = False
197+
__init_lock = threading.Lock()
198+
195199
def __init__(self):
196200
super().__init__()
197201

198202
def Init(self, id: int, networkInterface: str = None, qos: Qos = None):
199-
config = None
200-
# choose config
201-
if networkInterface is None:
202-
config = ChannelConfigAutoDetermine
203-
else:
204-
config = ChannelConfigHasInterface.replace('$__IF_NAME__$', networkInterface)
205-
206-
try:
207-
self.__domain = Domain(id, config)
208-
except DDSException as e:
209-
print("[ChannelFactory] create domain error. msg:", e.msg)
210-
return False
211-
except:
212-
print("[ChannelFactory] create domain error.")
213-
return False
214-
215-
try:
216-
self.__participant = DomainParticipant(id)
217-
except DDSException as e:
218-
print("[ChannelFactory] create domain participant error. msg:", e.msg)
219-
return False
220-
except:
221-
print("[ChannelFactory] create domain participant error")
222-
return False
223-
224-
self.__qos = qos
225-
226-
return True
203+
if self.__class__.__initialized:
204+
return True
205+
206+
with self.__class__.__init_lock:
207+
if self.__class__.__initialized:
208+
return True
209+
210+
config = None
211+
# choose config
212+
if networkInterface is None:
213+
config = ChannelConfigAutoDetermine
214+
else:
215+
config = ChannelConfigHasInterface.replace('$__IF_NAME__$', networkInterface)
216+
217+
try:
218+
self.__class__.__domain = Domain(id, config)
219+
except DDSException as e:
220+
print("[ChannelFactory] create domain error. msg:", e.msg)
221+
return False
222+
except:
223+
print("[ChannelFactory] create domain error.")
224+
return False
225+
226+
try:
227+
self.__class__.__participant = DomainParticipant(id)
228+
except DDSException as e:
229+
print("[ChannelFactory] create domain participant error. msg:", e.msg)
230+
return False
231+
except:
232+
print("[ChannelFactory] create domain participant error")
233+
return False
234+
235+
self.__class__.__qos = qos
236+
self.__class__.__initialized = True
237+
return True
227238

228239
def CreateChannel(self, name: str, type: Any):
229-
return Channel(self.__participant, name, type, self.__qos)
240+
return Channel(self.__class__.__participant, name, type, self.__class__.__qos)
230241

231242
def CreateSendChannel(self, name: str, type: Any):
232243
channel = self.CreateChannel(name, type)

0 commit comments

Comments
 (0)