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

[ROS2]Is it possible to have callbacks on different threads in one node?

asked 2021-01-28 11:39:15 -0600

wngfra gravatar image

Hello mates,

I am having a node with both service and subscriber. While the service call executes an external controller for quite a long time, it blocks the callback for the subscriber. Is there any way that the callback function for the subscriber could be run on a different thread so that both callbacks function simultaneously?

FYI, previously I tried to run a separate node for the subscriber but my controller needs the real-time update of data from the subscriber callback. A separate subscriber node forced me to pass the data class two times, which is troublesome when I need to modify some codes later.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2021-01-29 09:17:14 -0600

APettinger gravatar image

updated 2021-01-29 09:25:41 -0600

It sounds like you want an async spinner. See this article for a pretty thorough example. I will also call out this quote from the Conclusion. I do believe it is good advice:

A good practice is to always keep your callback functions fast

EDIT: Did not see your [ROS2] tag until I posted. Check out multithreaded_executor's in ROS2

edit flag offensive delete link more


Thanks a lot for your info. I'll take a look.

wngfra gravatar image wngfra  ( 2021-02-01 05:18:39 -0600 )edit

It's important to know even if you have multiple threads running callbacks, if they use the same callback group they won't run in parallel because it defaults to only allowing one to run at time. See my answer to a similar question for more details:

jdlangs gravatar image jdlangs  ( 2021-02-02 09:34:58 -0600 )edit

Thanks for your reminder! Therefore the critical point is to assign callbacks to different groups and use a multithreaded_executor, right?

wngfra gravatar image wngfra  ( 2021-02-02 12:21:00 -0600 )edit

Question Tools



Asked: 2021-01-28 11:37:06 -0600

Seen: 2,777 times

Last updated: Jan 29 '21