ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Is rospython subscriber thread safe?

asked 2019-01-21 06:05:45 -0600

jlyw1017 gravatar image

I'm trying to write a python node, which has one class and two member functions as callback functions.
so a TimeSynchronizer and Subscriber in a node.
I saw in this link and tested that python subscribers are event-driven model and are in separate threads.

My question here is , are they thread safe when they are working on the same class member? e.g. self.a below. Do I need to write a lock? Where can I find more information about this?
Thank you in advanced!!!!

class mr_listener(object):

    def __init__(self):
        self.a =  [1,2,3,4,5]
        rospy.init_node('mr_listener', anonymous=True)
        scan_subscriber = message_filters.Subscriber('/scan',LaserScan)
        pose_subscriber = message_filters.Subscriber('/slam_out_pose', PoseStamped)
        ts = message_filters.ApproximateTimeSynchronizer([scan_subscriber,projected_scan_subscriber,pose_subscriber], 10, 0.1, allow_headerless=True)
        rospy.Subscriber("chatter", String, self.callback2)

    def callback1(self,scan,pose):

    def callback2(self,chatt):
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2019-06-11 03:22:12 -0600

janindu gravatar image

@stevemartin 's answer is incorrect.

roscpp Single Threaded Spinning is thread safe.

However, your question is on rospy. rospy subscribers have their own threads. I'm not clear if each subscriber runs on a separate thread, or if all subscribers for a given topic runs on a single thread and subscribers on different topics run on separate threads. Haven't been able to find that in documentation.

Bottom line, you can't assume thread safety in rospy. Use mutual exclusion to access shared resources in callback functions.

edit flag offensive delete link more

answered 2019-01-21 07:34:59 -0600

stevemartin gravatar image

Yes, ros nodes are threadsafe. You can read more about it here: LINK

edit flag offensive delete link more


OP asked about nodes written in Python i.e. using the client library rospy not roscpp, they don't share the same mechanics.

Delb gravatar image Delb  ( 2019-01-21 07:50:33 -0600 )edit

@Delb in terms of thread-safeness, they do

stevemartin gravatar image stevemartin  ( 2019-01-21 07:57:00 -0600 )edit

Question Tools



Asked: 2019-01-21 06:05:45 -0600

Seen: 1,881 times

Last updated: Jun 11 '19