[ROS2]Is it possible to have callbacks on different threads in one node?
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.
Asked by wngfra on 2021-01-28 12:37:06 UTC
Answers
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
Asked by APettinger on 2021-01-29 10:17:14 UTC
Comments
Thanks a lot for your info. I'll take a look.
Asked by wngfra on 2021-02-01 06:18:39 UTC
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: https://answers.ros.org/question/343279/ros2-how-to-implement-a-sync-service-client-in-a-node/?answer=354359#post-id-354359
Asked by jdlangs on 2021-02-02 10:34:58 UTC
Thanks for your reminder! Therefore the critical point is to assign callbacks to different groups and use a multithreaded_executor, right?
Asked by wngfra on 2021-02-02 13:21:00 UTC
Comments