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

Is rospython subscriber thread safe?

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

jlyw1017 gravatar image

Hey
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.
https://robotics.stackexchange.com/qu...

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)
        ts.registerCallback(self.callback1)
        rospy.Subscriber("chatter", String, self.callback2)
        rospy.spin()

    def callback1(self,scan,pose):
        somthing

    def callback2(self,chatt):
        somingthing
        rospy.sleep(1.)
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

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

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
1

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

stevemartin gravatar image

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

edit flag offensive delete link more

Comments

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 -0500 )edit

@Delb in terms of thread-safeness, they do

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

Question Tools

2 followers

Stats

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

Seen: 1,937 times

Last updated: Jun 11 '19