Threads when using ros2 and python
Hello, I want to implement a planner, which needs continous updates from other vehicles. Therefore the planner should be started in its own thread. I have read that in rospy with ROS1 each timer callback created a own thread. So I tried this in ROS2 as well, but it seems, that my program is stuck in the timer callback, where I create my planner. Is there another way to archive this?
class UploadPlan(Node):
def __init__(self):
super().__init__('upload_plan')
self.publisher_ = self.create_publisher(String, '/topic', 10)
self.subscription = self.create_subscription(
String,
'/topic',
self.sub_callback,
10)
timer_period = 1 # seconds
self.timer = self.create_timer(timer_period, self.timer_callback)
def timer_callback(self):
blocking_function() # needs to be in its own thread
self.timer.cancel()
def sub_callback(self, msg):
print("callback")
def main(args=None):
rclpy.init(args=args)
upload_plan = UploadPlan()
rclpy.spin(upload_plan)
upload_plan.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
Thanks.
There's definitely ways to achieve what you want to do. Please edit your question to provide a compact example for us to be able to help you.
I added an example
Why does the planner calculation need to be in its own thread?