Skip to content

Commit

Permalink
Add optional timeout to client.call()
Browse files Browse the repository at this point in the history
Signed-off-by: Muhong Guo <[email protected]>
  • Loading branch information
MuhongGuo committed Jan 6, 2022
1 parent 59efef8 commit 5881c44
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions rclpy/rclpy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ def __init__(
# True when the callback is ready to fire but has not been "taken" by an executor
self._executor_event = False

def call(self, request: SrvTypeRequest) -> SrvTypeResponse:
def call(self, request: SrvTypeRequest, timeout_sec: float = None) -> SrvTypeResponse:
"""
Make a service request and wait for the result.
.. warning:: Do not call this method in a callback or a deadlock may occur.
:param request: The service request.
:return: The service response.
:param timeout_sec: Seconds to wait. If ``None``, then wait forever.
:return: The service response if it doesn't timeout, ``None`` otherwise.
:raises: TypeError if the type of the passed request isn't an instance
of the Request type of the provided service when the client was
constructed.
Expand All @@ -92,7 +93,7 @@ def unblock(future):
# The callback might have been added after the future is completed,
# resulting in the event never being set.
if not future.done():
event.wait()
event.wait(timeout_sec)
if future.exception() is not None:
raise future.exception()
return future.result()
Expand Down

0 comments on commit 5881c44

Please sign in to comment.