Execution and Callbacks
There are two components that control the execution of callbacks: executors and callback groups.
Executors are responsible for the actual execution of callbacks and should extend the Executor
class.
Callback groups are used to enforce concurrency rules for callbacks and should extend the CallbackGroup
class.
Executors
- exception rclpy.executors.ConditionReachedException
Future has been completed.
- class rclpy.executors.Executor(*, context: Context | None = None)
The base class for an executor.
An executor controls the threading model used to process callbacks. Callbacks are units of work like subscription callbacks, timer callbacks, service calls, and received client responses. An executor controls which threads callbacks get executed in.
A custom executor must define
spin_once()
. If the executor has any cleanup then it should also defineshutdown()
.- Parameters:
context – The context to be associated with, or
None
for the default global context.
- add_node(node: Node) bool
Add a node whose callbacks should be managed by this executor.
- Parameters:
node – The node to add to the executor.
- Returns:
True
if the node was added,False
otherwise.
- can_execute(entity: WaitableEntityType) bool
Determine if a callback for an entity can be executed.
- Parameters:
entity – Subscription, Timer, Guard condition, etc
- Returns:
True
if the entity callback can be executed,False
otherwise.
- create_task(callback: Callable | Coroutine, *args, **kwargs) Task
Add a callback or coroutine to be executed during
spin()
and return a Future.Arguments to this function are passed to the callback.
- Parameters:
callback – A callback to be run in the executor.
- get_nodes() List[Node]
Return nodes that have been added to this executor.
- remove_node(node: Node) None
Stop managing this node’s callbacks.
- Parameters:
node – The node to remove from the executor.
- shutdown(timeout_sec: float | None = None) bool
Stop executing callbacks and wait for their completion.
- Parameters:
timeout_sec – Seconds to wait. Block forever if
None
or negative. Don’t wait if 0.- Returns:
True
if all outstanding callbacks finished executing, orFalse
if the timeot expires before all outstanding work is done.
- spin() None
Execute callbacks until shutdown.
- spin_once(timeout_sec: float | None = None) None
Wait for and execute a single callback.
A custom executor should use
wait_for_ready_callbacks()
to get work.- Parameters:
timeout_sec – Seconds to wait. Block forever if
None
or negative. Don’t wait if 0.
- spin_once_until_future_complete(future: Future, timeout_sec: float | TimeoutObject | None = None) None
Wait for and execute a single callback.
This should behave in the same way as
spin_once()
. If needed by the implementation, it should awake other threads waiting.- Parameters:
future – The executor will wait until this future is done.
timeout_sec – Maximum seconds to wait. Block forever if
None
or negative. Don’t wait if 0.
- spin_until_future_complete(future: Future, timeout_sec: float | None = None) None
Execute callbacks until a given future is done or a timeout occurs.
- wait_for_ready_callbacks(*args, **kwargs) Tuple[Task, WaitableEntityType, Node]
Return callbacks that are ready to be executed.
The arguments to this function are passed to the internal method
_wait_for_ready_callbacks()
to get a generator for ready callbacks:- _wait_for_ready_callbacks(timeout_sec: float | ~rclpy.executors.TimeoutObject | None = None, nodes: ~typing.List[Node] = None, condition: ~typing.Callable[[], bool] = <function Executor.<lambda>>) Generator[Tuple[Task, WaitableEntityType, Node], None, None]
Yield callbacks that are ready to be executed.
- Raises:
TimeoutException – on timeout.
ShutdownException – on if executor was shut down.
- Parameters:
timeout_sec – Seconds to wait. Block forever if
None
or negative. Don’t wait if 0.nodes – A list of nodes to wait on. Wait on all nodes if
None
.condition – A callable that makes the function return immediately when it evaluates to True.
- wake() None
Wake the executor because something changed.
This is used to tell the executor when entities are created or destroyed.
- exception rclpy.executors.ExternalShutdownException
Context has been shutdown.
- class rclpy.executors.MultiThreadedExecutor(num_threads: int | None = None, *, context: Context | None = None)
Runs callbacks in a pool of threads.
- Parameters:
num_threads – number of worker threads in the pool. If
None
, the number of threads will be automatically set by querying the underlying OS for the CPU affinity of the process space. If the OS doesn’t provide this information, defaults to 2.context – The context associated with the executor.
- spin_once(timeout_sec: float | None = None) None
Wait for and execute a single callback.
A custom executor should use
wait_for_ready_callbacks()
to get work.- Parameters:
timeout_sec – Seconds to wait. Block forever if
None
or negative. Don’t wait if 0.
- spin_once_until_future_complete(future: Future, timeout_sec: float | TimeoutObject | None = None) None
Wait for and execute a single callback.
This should behave in the same way as
spin_once()
. If needed by the implementation, it should awake other threads waiting.- Parameters:
future – The executor will wait until this future is done.
timeout_sec – Maximum seconds to wait. Block forever if
None
or negative. Don’t wait if 0.
- exception rclpy.executors.ShutdownException
Signal that executor was shut down.
- class rclpy.executors.SingleThreadedExecutor(*, context: Context | None = None)
Runs callbacks in the thread that calls
Executor.spin()
.- spin_once(timeout_sec: float | None = None) None
Wait for and execute a single callback.
A custom executor should use
wait_for_ready_callbacks()
to get work.- Parameters:
timeout_sec – Seconds to wait. Block forever if
None
or negative. Don’t wait if 0.
- spin_once_until_future_complete(future: Future, timeout_sec: float | TimeoutObject | None = None) None
Wait for and execute a single callback.
This should behave in the same way as
spin_once()
. If needed by the implementation, it should awake other threads waiting.- Parameters:
future – The executor will wait until this future is done.
timeout_sec – Maximum seconds to wait. Block forever if
None
or negative. Don’t wait if 0.
- exception rclpy.executors.TimeoutException
Signal that a timeout occurred.
- class rclpy.executors.TimeoutObject(timeout: float)
Use timeout object to save timeout.
- property timeout
- async rclpy.executors.await_or_execute(callback: Callable | Coroutine, *args) Any
Await a callback if it is a coroutine, else execute it.
Callback Groups
- class rclpy.callback_groups.CallbackGroup
The base class for a callback group.
A callback group controls when callbacks are allowed to be executed.
This class should not be instantiated. Instead, classes should extend it and implement
can_execute()
,beginning_execution()
, andending_execution()
.- add_entity(entity) None
Add an entity to the callback group.
- Parameters:
entity – a subscription, timer, client, service, or waitable instance.
- beginning_execution(entity) bool
Get permission for the callback from the group to begin executing an entity.
If this returns
True
thenCallbackGroup.ending_execution()
must be called after the callback has been executed.- Parameters:
entity – a subscription, timer, client, service, or waitable instance.
- Returns:
True
if the callback can be executed,False
otherwise.
- can_execute(entity) bool
Determine if an entity can be executed.
- Parameters:
entity – a subscription, timer, client, service, or waitable instance.
- Returns:
True
if the entity can be executed,False
otherwise.
- ending_execution(entity) None
Notify group that a callback has finished executing.
- Parameters:
entity – a subscription, timer, client, service, or waitable instance.
- has_entity(entity) bool
Determine if an entity has been added to this group.
- Parameters:
entity – a subscription, timer, client, service, or waitable instance.
- class rclpy.callback_groups.MutuallyExclusiveCallbackGroup
Allow only one callback to be executing at a time.
- beginning_execution(entity)
Get permission for the callback from the group to begin executing an entity.
If this returns
True
thenCallbackGroup.ending_execution()
must be called after the callback has been executed.- Parameters:
entity – a subscription, timer, client, service, or waitable instance.
- Returns:
True
if the callback can be executed,False
otherwise.
- can_execute(entity)
Determine if an entity can be executed.
- Parameters:
entity – a subscription, timer, client, service, or waitable instance.
- Returns:
True
if the entity can be executed,False
otherwise.
- ending_execution(entity)
Notify group that a callback has finished executing.
- Parameters:
entity – a subscription, timer, client, service, or waitable instance.
- class rclpy.callback_groups.ReentrantCallbackGroup
Allow callbacks to be executed in parallel without restriction.
- beginning_execution(entity)
Get permission for the callback from the group to begin executing an entity.
If this returns
True
thenCallbackGroup.ending_execution()
must be called after the callback has been executed.- Parameters:
entity – a subscription, timer, client, service, or waitable instance.
- Returns:
True
if the callback can be executed,False
otherwise.
- can_execute(entity)
Determine if an entity can be executed.
- Parameters:
entity – a subscription, timer, client, service, or waitable instance.
- Returns:
True
if the entity can be executed,False
otherwise.
- ending_execution(entity)
Notify group that a callback has finished executing.
- Parameters:
entity – a subscription, timer, client, service, or waitable instance.