-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Michel Hidalgo <[email protected]>
- Loading branch information
1 parent
a1273c7
commit d907d62
Showing
8 changed files
with
195 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,27 @@ | ||
# Copyright (c) 2023 Boston Dynamics AI Institute Inc. All rights reserved. | ||
from threading import Event | ||
from typing import Any, Optional | ||
from typing import Optional | ||
|
||
from rclpy.context import Context | ||
from rclpy.task import Future | ||
from rclpy.utilities import get_default_context | ||
|
||
|
||
def wait_for_future(future: Future, timeout_sec: Optional[float] = None) -> bool: | ||
def wait_for_future(future: Future, timeout_sec: Optional[float] = None, *, context: Optional[Context] = None) -> bool: | ||
"""Blocks while waiting for a future to become done | ||
Args: | ||
future (Future): The future to be waited on | ||
timeout_sec (Optional[float]): An optional timeout for how long to wait | ||
context (Optional[Context]): Current context (will use the default if none is given) | ||
Returns: | ||
bool: True if successful, False if the timeout was triggered | ||
""" | ||
if context is None: | ||
context = get_default_context() | ||
event = Event() | ||
|
||
def done_callback(_: Any) -> None: | ||
nonlocal event | ||
event.set() | ||
|
||
future.add_done_callback(done_callback) | ||
return event.wait(timeout=timeout_sec) | ||
context.on_shutdown(event.set) | ||
future.add_done_callback(lambda _: event.set()) | ||
event.wait(timeout=timeout_sec) | ||
return future.done() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.