Executors — what really happens behind rclpy.spin()
Atlasbot · 17/8/2026 · 11 min read
Nearly all ROS2 nodes end in rclpy.spin(node), but few have noticed that this line actually creates and runs a hidden Executor underneath. The correct understanding of what Executor is explains both of the previous articles — why callback default is multithreading, and why the Callback Group controls parallelization.
Key concepts
An executor is an event loop that corrects three repeated operations:
- The wait-set — asks the class
rcl/DDS below: of the subscribers/timer/service/action registered with the node, which is having available data to process now? - Select the available callback— from the above list, selecting the qualified callbacks (depending on the Callback Group of each)
- Callback — executes the corresponding callback function, on stream of any kind of Executor (Single or MultiThreaded)
import rclpy
rclpy.init()
node = rclpy.create_node("my_node")
rclpy.spin(node) # create SingleThreadedExecutor, add node (), then infinite spin ()
rclpy.spin(node) is only a 3-line package utility function: create the default executor, add_node(node), call executor.spin() — write these three steps explicitly as the only way to choose the other type of executor instead of the default.
How it works
executor = rclpy.executors.SingleThreadedExecutor()
executor.add_node(node)
while rclpy.ok():
executor.spin_once(timeout_sec=0.1) # do the same round and return control
# can insert other code here between rounds
spin() runs indefinitely until the node is shutdown — which suits most cases. spin_once() processes only once and then returns control to your while loop — used when custom logic needs to be inserted between callback runs.
In summary: Executor is the layer intermediate between "data that has come from DDS" and "code callback of your actual run" — that determines the order and degree of parallelism, not DDS or rclpy doing it themselves.
Technical details
Parameters
Comparison of the three types of Executors available in rclpy:
| Type of Executor | Traits |
|---|---|
SingleThreadedExecutor | default, 1 thread, rebuild wait-set every loop |
MultiThreadedExecutor | Multiple Streams, allow parallel if Callback Group is appropriate |
StaticSingleThreadedExecutor | 1 thread, build wait-set 1 single time at initialization — faster but does not support add/drop of subscribers at runtime |
Practical applications
StaticSingleThreadedExecutor is best suited for production nodes whose publisher/subscriber/timer set is fixed at initialization, remains constant throughout the life cycle— typical of most node drivers/controllers on real-world robots (unlike in development, new subscribers are rarely needed at runtime). On resource-limited embedded computers, avoiding the cost of replaying the wait-set per loop (as SingleThreadedExecutor usually does) can make a significant difference to a node that needs very high frequency control loops.
Practical example
# Integration of ROS2 into a GUI app with its own event loop (e.g., PyQt)
# spin() will take full control — not usable here
# Instead, invoke the periodical spin `once() via the GUI QTimer
from PyQt5.QtCore import QTimer
class RosGuiApp:
def __init__(self, node):
self.node = node
self.executor = rclpy.executors.SingleThreadedExecutor()
self.executor.add_node(node)
self.timer = QTimer()
self.timer.timeout.connect(self.spin_ros)
self.timer.start(50) # invoke spin once every 50ms
def spin_ros(self):
self.executor.spin_once(timeout_sec=0)
Result: the GUI remains responsive (Qt's event loop is not occupied),
while ROS2 still processes callbacks regularly every 50ms — the 2 event loops
share control instead of competing.
This is the most popular real-world application of spin_once()— when ROS2 needs to "live together" with another framework that already has its own event loop.
Common mistakes
- Call
rclpy.spin()then question code then do not run —spin()block indefinitely until node shutdown — all code written after linerclpy.spin(node)inmain()runs only after the node has stopped, not parallel to the spin loop. spin_once(timeout_sec=0)causes busy-waiting, accounting for 100% of the CPU— timeout by zero which causes the executor to constantly check non-stop — so a small positive timeout (e.g.,0.01) should be used so that the executor "sleeps" a little between checks if nothing new is found.- Timeout is too long in the GUI integration loop— if
spin_once(timeout_sec=1.0)while the GUI needs much faster feedback, the interface will feel "fixed" during that time interval — chooses a timeout short enough compared to the frequency needed feedback of the rest of the application. - Only 1 node can be spin when more than one node is really needed —
rclpy.spin(node)the default manages only 1 node correctly — needs to create an explicit executor andadd_node()many times (learned in Multi-threading) if more than one node shares a common executor.
Conclusion
Executors are the actual "motor" behind rclpy.spin() — continuous build wait - sets, select callback available, and call them in accordance with Callback Group rules and the type of Executor in use. Properly understanding this mechanism explains the full behavior of both Multi-threading and Callback Group modules learned in the previous two videos. The next, and final, article of the entire ROS / ROS2 category, discusses DDS Configuration — further refining the middleware layer through which every last Executor must communicate.
Get new articles and lessons by email
When a new technical article or course module is published, Atlas Robotics sends you a short email. No spam.
Unsubscribe any time with the link in each email. See our Privacy policy.