From c009b0de286101da1caedef860b88f1520edb97b Mon Sep 17 00:00:00 2001 From: Barry Xu Date: Fri, 25 Oct 2024 19:48:29 +0800 Subject: [PATCH] Avoid redundant done callbacks of the future while repeatedly calling spin_until_future_complete (#1374) Signed-off-by: Barry Xu --- rclpy/rclpy/executors.py | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/rclpy/rclpy/executors.py b/rclpy/rclpy/executors.py index 17169063d..4ed931276 100644 --- a/rclpy/rclpy/executors.py +++ b/rclpy/rclpy/executors.py @@ -322,14 +322,14 @@ def spin_until_future_complete( if timeout_sec is None or timeout_sec < 0: while self._context.ok() and not future.done() and not self._is_shutdown: - self.spin_once_until_future_complete(future, timeout_sec) + self._spin_once_until_future_complete(future, timeout_sec) else: start = time.monotonic() end = start + timeout_sec timeout_left = TimeoutObject(timeout_sec) while self._context.ok() and not future.done() and not self._is_shutdown: - self.spin_once_until_future_complete(future, timeout_left) + self._spin_once_until_future_complete(future, timeout_left) now = time.monotonic() if now >= end: @@ -367,6 +367,13 @@ def spin_once_until_future_complete( """ raise NotImplementedError() + def _spin_once_until_future_complete( + self, + future: Future, + timeout_sec: Optional[Union[float, TimeoutObject]] = None + ) -> None: + raise NotImplementedError() + def _take_timer(self, tmr): try: with tmr.handle: @@ -852,13 +859,20 @@ def _spin_once_impl( def spin_once(self, timeout_sec: Optional[float] = None) -> None: self._spin_once_impl(timeout_sec) + def _spin_once_until_future_complete( + self, + future: Future, + timeout_sec: Optional[Union[float, TimeoutObject]] = None + ) -> None: + self._spin_once_impl(timeout_sec, future.done) + def spin_once_until_future_complete( self, future: Future, timeout_sec: Optional[Union[float, TimeoutObject]] = None ) -> None: future.add_done_callback(lambda x: self.wake()) - self._spin_once_impl(timeout_sec, future.done) + self._spin_once_until_future_complete(future, timeout_sec) class MultiThreadedExecutor(Executor): @@ -924,13 +938,20 @@ def _spin_once_impl( def spin_once(self, timeout_sec: Optional[float] = None) -> None: self._spin_once_impl(timeout_sec) + def _spin_once_until_future_complete( + self, + future: Future, + timeout_sec: Optional[Union[float, TimeoutObject]] = None + ) -> None: + self._spin_once_impl(timeout_sec, future.done) + def spin_once_until_future_complete( self, future: Future, timeout_sec: Optional[Union[float, TimeoutObject]] = None ) -> None: future.add_done_callback(lambda x: self.wake()) - self._spin_once_impl(timeout_sec, future.done) + self._spin_once_until_future_complete(future, timeout_sec) def shutdown( self,