Skip to content

Commit 73a60ad

Browse files
committed
Make topic_publisher server req async
1 parent 9dbf738 commit 73a60ad

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

mocha_core/mocha_core/topic_publisher.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,25 +67,29 @@ def run(self):
6767
msg_header = self.msg_queue.get(timeout=1)
6868
except queue.Empty:
6969
continue
70-
self.logger.info(f"{self.robot_name}{self.topic_name} - Topic Publisher - Loop")
70+
self.logger.debug(f"{self.robot_name}{self.topic_name} - Topic Publisher - Loop")
7171
req = mocha_core.srv.GetDataHeaderDB.Request()
7272
req.msg_header = msg_header
7373
future = self.get_data_header_db_cli.call_async(req)
74-
rclpy.spin_until_future_complete(
75-
self.ros_node, future, timeout_sec=2.0
76-
)
77-
if future.done():
78-
answ = future.result()
79-
else:
80-
self.logger.warning(f"{self.robot_name} - Topic Publisher - get_data_header_db service call timeout")
81-
continue
82-
ans_robot_id, ans_topic_id, ans_ts, ans_data = du.parse_answer(
83-
answ, self.msg_types
84-
)
85-
self.publisher.publish(ans_data)
74+
75+
def handle_response(future):
76+
try:
77+
if future.done():
78+
answ = future.result()
79+
ans_robot_id, ans_topic_id, ans_ts, ans_data = du.parse_answer(
80+
answ, self.msg_types
81+
)
82+
self.publisher.publish(ans_data)
83+
# self.msg_queue.task_done()
84+
else:
85+
self.logger.warning(f"{self.robot_name} - Topic Publisher - get_data_header_db service call timeout")
86+
except Exception as e:
87+
self.logger.error(f"{self.robot_name} - Topic Publisher - Error handling service response: {e}")
88+
89+
future.add_done_callback(handle_response)
8690

8791
def get_message(self, msg_header):
88-
self.logger.warning(f"{self.robot_name} - Topic Publisher - Callback")
92+
self.logger.debug(f"{self.robot_name} - Topic Publisher - Callback")
8993
self.msg_queue.put(msg_header)
9094

9195
def shutdown(self):
@@ -180,10 +184,10 @@ def __init__(self, this_robot=None, robot_configs=None, topic_configs=None):
180184
self.list_of_threads[robot_id][topic_id] = tp
181185
tp.th.start()
182186
sub = self.create_subscription(DatabaseCB, "/mocha/database_cb",
183-
self.msgheader_callback, 10)
187+
self.msgheader_callback, 10,
188+
callback_group=self.callback_group)
184189

185190
def msgheader_callback(self, data):
186-
pdb.set_trace()
187191
robot_id = data.robot_id
188192
topic_id = data.topic_id
189193
msg_header = data.msg_header

0 commit comments

Comments
 (0)