[ros2] Why does sleep not work inside callback?
I noticed that sleep does not work inside callback in default settings, i.e., SingleThreadedExecutor
with MutuallyExclusiveCallbackGroup
. Please see a sample code snippet below:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from example_interfaces.action import Fibonacci
import rclpy
from rclpy.action import ActionServer
from rclpy.node import Node
class MinimalActionServer(Node):
def __init__(self):
super().__init__('minimal_action_server')
self.as_ = ActionServer(self, Fibonacci, 'fib', execute_callback=self.execute_cb)
def execute_cb(self, goal_handle):
fb = Fibonacci.Feedback(sequence=[0, 1])
loop_rate = self.create_rate(1, self.get_clock()) # 1 Hz
for i in range(1, goal_handle.request.order):
self.get_logger().info(f'[{i}] Executing goal...')
fb.sequence.append(fb.sequence[i] + fb.sequence[i - 1])
self.get_logger().info(f'Publishing feedback: {fb.sequence}')
goal_handle.publish_feedback(fb)
loop_rate.sleep() # commenting this line makes the callback work
goal_handle.succeed()
return Fibonacci.Result(sequence=fb.sequence)
def main(args=None):
rclpy.init(args=args)
node = MinimalActionServer()
try:
rclpy.spin(node)
except KeyboardInterrupt:
pass
finally:
rclpy.shutdown()
if __name__ == '__main__':
main()
Question
Why does sleep not work inside callback?
Additional Info
- Operating System: Ubuntu 20.04.4 LTS
- Installation type: binaries
- Version or commit hash: ROS Galactic
- DDS implementation: Default
- Client library: rclpy