Skip to content

Commit c009b0d

Browse files
Avoid redundant done callbacks of the future while repeatedly calling spin_until_future_complete (#1374)
Signed-off-by: Barry Xu <[email protected]>
1 parent a09a031 commit c009b0d

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

rclpy/rclpy/executors.py

+25-4
Original file line numberDiff line numberDiff line change
@@ -322,14 +322,14 @@ def spin_until_future_complete(
322322

323323
if timeout_sec is None or timeout_sec < 0:
324324
while self._context.ok() and not future.done() and not self._is_shutdown:
325-
self.spin_once_until_future_complete(future, timeout_sec)
325+
self._spin_once_until_future_complete(future, timeout_sec)
326326
else:
327327
start = time.monotonic()
328328
end = start + timeout_sec
329329
timeout_left = TimeoutObject(timeout_sec)
330330

331331
while self._context.ok() and not future.done() and not self._is_shutdown:
332-
self.spin_once_until_future_complete(future, timeout_left)
332+
self._spin_once_until_future_complete(future, timeout_left)
333333
now = time.monotonic()
334334

335335
if now >= end:
@@ -367,6 +367,13 @@ def spin_once_until_future_complete(
367367
"""
368368
raise NotImplementedError()
369369

370+
def _spin_once_until_future_complete(
371+
self,
372+
future: Future,
373+
timeout_sec: Optional[Union[float, TimeoutObject]] = None
374+
) -> None:
375+
raise NotImplementedError()
376+
370377
def _take_timer(self, tmr):
371378
try:
372379
with tmr.handle:
@@ -852,13 +859,20 @@ def _spin_once_impl(
852859
def spin_once(self, timeout_sec: Optional[float] = None) -> None:
853860
self._spin_once_impl(timeout_sec)
854861

862+
def _spin_once_until_future_complete(
863+
self,
864+
future: Future,
865+
timeout_sec: Optional[Union[float, TimeoutObject]] = None
866+
) -> None:
867+
self._spin_once_impl(timeout_sec, future.done)
868+
855869
def spin_once_until_future_complete(
856870
self,
857871
future: Future,
858872
timeout_sec: Optional[Union[float, TimeoutObject]] = None
859873
) -> None:
860874
future.add_done_callback(lambda x: self.wake())
861-
self._spin_once_impl(timeout_sec, future.done)
875+
self._spin_once_until_future_complete(future, timeout_sec)
862876

863877

864878
class MultiThreadedExecutor(Executor):
@@ -924,13 +938,20 @@ def _spin_once_impl(
924938
def spin_once(self, timeout_sec: Optional[float] = None) -> None:
925939
self._spin_once_impl(timeout_sec)
926940

941+
def _spin_once_until_future_complete(
942+
self,
943+
future: Future,
944+
timeout_sec: Optional[Union[float, TimeoutObject]] = None
945+
) -> None:
946+
self._spin_once_impl(timeout_sec, future.done)
947+
927948
def spin_once_until_future_complete(
928949
self,
929950
future: Future,
930951
timeout_sec: Optional[Union[float, TimeoutObject]] = None
931952
) -> None:
932953
future.add_done_callback(lambda x: self.wake())
933-
self._spin_once_impl(timeout_sec, future.done)
954+
self._spin_once_until_future_complete(future, timeout_sec)
934955

935956
def shutdown(
936957
self,

0 commit comments

Comments
 (0)