Skip to content

Commit

Permalink
Avoid redundant done callbacks of the future while repeatedly calling…
Browse files Browse the repository at this point in the history
… spin_until_future_complete (#1374)

Signed-off-by: Barry Xu <[email protected]>
  • Loading branch information
Barry-Xu-2018 authored Oct 25, 2024
1 parent a09a031 commit c009b0d
Showing 1 changed file with 25 additions and 4 deletions.
29 changes: 25 additions & 4 deletions rclpy/rclpy/executors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c009b0d

Please sign in to comment.