-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rework tf listener wrapper #35
Merged
mhidalgo-bdai
merged 6 commits into
bdaiinstitute:main
from
mhidalgo-bdai:rework-tf-listener-wrapper
Oct 19, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
eb45b94
Rework futures to be context aware
mhidalgo-bdai 561ada3
Rework TFListenerWrapper API
mhidalgo-bdai 25bc002
Fix wait_for_frames semantics
mhidalgo-bdai 49ee392
Fix lookup_latest_timestamp
mhidalgo-bdai fc71daa
Improve TFListenerWrapper documentation
mhidalgo-bdai 6c45bd6
Change warning condition
mhidalgo-bdai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if the context has multiple events set and shuts down? Is there a possibility where two events can be waiting on each other and deadlock?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the context has multiple events, all of them will be set. Events do not wait on each other. Callees wait on events. Callees could run into deadlock if, for example, they have to complete each other futures or you somehow manage to wait on a future within an on context shutdown handler (which I'm not even sure if the Context API will allow).
So I would say deadlocks are a non-issue here inasmuch as it won't happen by accident unless code is flawed.