Robotics StackExchange | Archived questions

ros2 publisher thread safe

Hello together,

atm i wonder if the foo.publish() method of a ROS2 publisher is threadsafe. I would guess yes, as long we hand over a copy of the msg, but maybe a more advanced user could confirm me that. From my understanding the publish() method is Threadsafe, since all operations are python buildin functions ( beside rclpy.rclpypublish(capsule, msg) and no membervariables are used.

https://github.com/ros2/rclpy/blob/master/rclpy/rclpy/publisher.py

   def publish(self, msg: MsgType) -> None:
    """
    Send a message to the topic for the publisher.
    :param msg: The ROS message to publish.
    :raises: TypeError if the type of the passed message isn't an instance
      of the provided type when the publisher was constructed.
    """
    if not isinstance(msg, self.msg_type):
        raise TypeError()
    with self.handle as capsule:
        _rclpy.rclpy_publish(capsule, msg)

For the Handle Class tho, i am missing knowledge to understand it. i guess the description is a good hint.

https://github.com/ros2/rclpy/blob/30eeae9b30ab15209798d62b501d1dec633e1db3/rclpy/rclpy/handle.py#L25

class Handle:
"""
Wrap a pycapsule object for thread-safe early destruction.

1) But since the msg is not copied at any point, modifieng the msg is possible until "rclpy.rclpypublish(capsule, msg)" is called.

2) If multible threads are using the same publisher, will this line "with self.handle as capsule:" handle that?

Are my assumptions correct?

best regards

Asked by mgangl on 2020-01-27 06:28:51 UTC

Comments

Answers