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

Thread safety and rospy Timer

asked 2015-11-11 15:54:23 -0500

etappan gravatar image

I'm a little confused by how rospy.Timer works with regards to thread-safety. My understanding of callbacks in rospy in general is they're essentially handled sequentially during calls to Sleep and Spin, meaning they're implicitly threadsafe and I can modify the same data from multiple callbacks within a node without locks or other safeguards.

Timers represent a totally separate thread of execution, albeit one that still runs off of a Rate.sleep() loop. Is it safe to work with data that a Timer interacts with, or do I need to start adding locks in any nodes that have both Subscriptions and Timers?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2017-08-10 17:44:05 -0500

Ed Venator gravatar image

My understanding of callbacks in rospy in general is they're essentially handled sequentially during calls to Sleep and Spin, meaning they're implicitly threadsafe and I can modify the same data from multiple callbacks within a node without locks or other safeguards.

Unfortunately, no. Unlike roscpp, rospy creates a new thread for every Subscriber and Timer. In fact, rospy.spin() is just an infinite loop of half-second waits. As such, callbacks in rospy are not threadsafe. You should use locks in any rospy node where multiple callbacks of any kind might interact.

edit flag offensive delete link more

Question Tools

3 followers

Stats

Asked: 2015-11-11 15:54:23 -0500

Seen: 2,056 times

Last updated: Aug 10 '17