ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Please explain how the Subscriber works

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

Yehor gravatar image


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

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

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

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


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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



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

Seen: 704 times

Last updated: Mar 20 '19