ros2 publisher thread safe

asked 2020-01-27 05:28:51 -0500

mgangl gravatar image

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.rclpy_publish(capsule, msg) and no membervariables are used.

https://github.com/ros2/rclpy/blob/ma...

   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/30...

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.rclpy_publish(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

edit retag flag offensive close merge delete