-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cancelling asynchronous tasks has no effect?
ros2/rclpy#1099 Signed-off-by: Tomoya Fujita <[email protected]>
- Loading branch information
1 parent
bdd3410
commit fb33e32
Showing
3 changed files
with
88 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
|
||
import rclpy | ||
from rclpy.node import Node | ||
from rclpy.task import Future | ||
from rclpy.callback_groups import MutuallyExclusiveCallbackGroup | ||
|
||
|
||
def sleep_async(node: Node, time: float) -> Future: | ||
""" | ||
Sleeps for a given amount of time. | ||
Creates a timer with the sleep time as frequency and destroys it on the first callback. | ||
args: | ||
node: The node to sleep on. Future will be created with the nodes executor. | ||
time: The time to sleep in seconds | ||
returns: | ||
A future that will be done after the given amount of time. | ||
""" | ||
future = Future(executor=node.executor) | ||
timer = node.create_timer(timer_period_sec=time, callback=lambda: _timer_callback(future), callback_group=MutuallyExclusiveCallbackGroup()) | ||
|
||
def _timer_callback(future: Future): | ||
future.set_result(None) | ||
node.destroy_timer(timer) | ||
|
||
return future | ||
|
||
async def sleep(self, seconds): | ||
for i in range(1, seconds+1): | ||
await sleep_async(self, 1) | ||
print(f"Slept for {i} seconds") | ||
|
||
|
||
def main(args=None): | ||
rclpy.init() | ||
|
||
node = rclpy.create_node("test") | ||
executor = rclpy.get_global_executor() | ||
executor.add_node(node) | ||
task = executor.create_task(sleep, node, 5) | ||
|
||
task.cancel() | ||
print(f"{task.cancelled()=}") | ||
executor.spin_until_future_complete(task) | ||
|
||
print(f"{task.done()=}") | ||
print(f"{task.cancelled()=}") | ||
|
||
if __name__ == '__main__': | ||
main() |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import rclpy | ||
|
||
|
||
async def test_cancel(): | ||
future = rclpy.task.Future() | ||
future.cancel() | ||
|
||
print(future, future.cancelled(), future.done()) | ||
|
||
return await future | ||
|
||
|
||
def main(args=None): | ||
rclpy.init() | ||
|
||
node = rclpy.create_node("test") | ||
|
||
def timer_callback(): | ||
node.get_logger().info("Logging timer fired.") | ||
|
||
node.create_timer(1.0, timer_callback) | ||
|
||
executor = rclpy.get_global_executor() | ||
executor.add_node(node) | ||
task = executor.create_task(test_cancel) | ||
|
||
print(f"{task.cancelled()=}") | ||
#task.cancel() | ||
executor.spin_until_future_complete(task) | ||
print(f"{task.done()=}") | ||
print(f"{task.cancelled()=}") | ||
|
||
if __name__ == '__main__': | ||
main() |