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

Please explain how the Subscriber works

asked 2019-03-20 09:34:44 -0600

Yehor gravatar image

Hello,

Could someone explain to me how the rospy.Subscriber('topic', msg, callback) method works. As I noticed we don't need to loop that method to update and check the topic iteratively. It is doing it by itself, and I can't understand how.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
6

answered 2019-03-20 10:29:02 -0600

updated 2019-03-20 10:39:18 -0600

The ROS python library runs additional threads to handle message subscription and publishing. So when you call rospy.Subscribe in your code, it doesn't check for incoming messages and execute callbacks itself. It simply adds the topic and function to a list. This list is regularly checked in a different thread which executes the callbacks as needed. This is why you don't need any polling in your code to make the message callbacks work.

Hope this makes sense.

edit flag offensive delete link more

Comments

So, whenever ros topic will be updated, the Subscriber will get it and launch the callback function? Without any looping.

Yehor gravatar image Yehor  ( 2019-03-20 12:50:11 -0600 )edit

Strictly speaking topics are not updated, because they have no state, they are channels for messages to be passed through. So think of a message being received as opposed to an update. But you are correct, the Subscriber object will make sure the callback is executed when a message is received. You don't need any loops in your code to make this happen, there are loops but they are hidden with rospy so you don't need to worry about them.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2019-03-21 06:53:20 -0600 )edit

Wait, the Python descriptor causes there to be no actual "thread" running in the OS. It just fakes the multi-threading by switching between Python threads. Is it still the same as the subscriber callback function is called whenever rospy thinks it needs to be called?

Scibot gravatar image Scibot  ( 2023-05-21 01:27:45 -0600 )edit

Question Tools

3 followers

Stats

Asked: 2019-03-20 09:34:44 -0600

Seen: 989 times

Last updated: Mar 20 '19