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

What happens when two messages from different topics came one after another?

asked 2020-05-19 07:02:22 -0500

Kansai gravatar image

A bit related to this question, I would like to ask

what happens when two messages from different topics a node has subscribed come one after another?

This time a node is subscribed to two (or more) topics. Every time a message comes it will call a callback where it will process the message.

So, a message comes in and the node calls the callback function, process the message and finishes. Then a second message perhaps from another topic comes in , the node calls the appropriate callback. So far no problem.

But what happens if when the node is still processing the topic ONE through the callback ONE, a message for topic TWO arrives? Does the node waits for ONE to be processed or does the callback TWO starts in a different thread? How does this work?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2020-05-20 02:56:25 -0500

MCornelis gravatar image

The other answer is correct, but I would like to post a more complete global overview of what the executor is doing in the background. I feel like this is a big mystery to a lot of people working with ROS2.

For clarification I'm talking about the SingleThreadedExecutor that was in ROS2 before the Foxy release. The static executor released in Foxy works slightly differently.

How the executor works:

During Node initialization handles to nodes, callbackgroups, timers, subscriptions etc. are created in RCL and RMW. The information contained in a Node is added to the executor using a weak pointer.

start of a spin()

  1. The executor finds nothing to execute
  2. The executor performs collect_entities (gets handles from RCL)
  3. The executor creates a wait_set using these handles
  4. The executor communicates with RMW (DDS layer) to see what callbacks are ready to execute. If something is not ready to execute replace the handle in the wait-set with NULL
  5. The NULL handles are removed from the wait_set. Meaning you are now left with a wait-set containing only things that need to be executed (during this spin) instead of all things that exist.
  6. The executor looks for the next thing to execute in the following order: timers, subscriptions, services, clients, waitables. So if a ready timer exists it will search for the corresponding timer-callback. If there are no timers it will execute the first subscription-callback it can find etc. To find a corresponding callback, the executor searches a tree of weak-ptrs to the nodes that are part of the executor.
  7. Once it finds the correct callback in the correct callbackgroup in the correct node it will execute it and remove the handle from the wait-set.
  8. As long as there are still things to execute, meaning the wait-set is not empty yet, repeat 6 till 7, if nothing is left to execute go to 1 (next spin cycle).

At the start of the next spin cycle, the wait-set is completely filled again, all elements are checked to see if they are ready and they are all executed in order again.

So like the other answer states. Yes, both callbacks will be executed. Do keep in mind that the RMW calls are only made at the start of a spin(). So any messages coming in while the executor is finishing spin1 (processing all the ready callbacks) will not be seen until spin2.

edit flag offensive delete link more

answered 2020-05-19 14:04:37 -0500

forbiddenfruit gravatar image

updated 2020-05-19 14:05:15 -0500

The node waits for ONE to be processed before going on to TWO! So if you check out the source code for executor (cpp or py whichever you are working with). There is no concurrency, so as long as you are NOT threading in the Node, the spin will handle it one at a time.

So in your case, if a node subscribes to multiple topics, it will read the message of one and finish it before going on to the next topic and next message and on and on.

edit flag offensive delete link more

Question Tools



Asked: 2020-05-19 07:02:22 -0500

Seen: 961 times

Last updated: May 20 '20