Use executors to manage callback execution
rollingrclpy provides standard executors, you can create specialized ones by extending rclpy.executors.Executor.repository·rolling·Indexed 19 days ago
https://github.com/ros2/rclpyThe ROS 2 Client Library for Python, providing interfaces to build ROS 2 nodes and communicate via topics, services, and actions. It includes modules for publishing and subscribing (including experimental async versions), service clients and servers, and action clients and servers. The library manages callback execution through Executors and Callback Groups to control concurrency and thread safety.
rclpy provides standard executors, you can create specialized ones by extending rclpy.executors.Executor.In rclpy, the execution of callbacks (such as timer callbacks, subscription callbacks, or service callbacks) is managed by two primary components:
rclpy.executors.Executor class.rclpy.callback_groups.CallbackGroup class.Together, executors pick up tasks and callback groups dictate how those tasks interact with each other regarding thread safety and concurrency.
rclpy.callback_groups.CallbackGroup.In rclpy, a Service Client is used to request a service from a Service Server. You can use the standard synchronous/asynchronous client or the experimental async client.
rclpy.client. Use this for typical service request/response patterns.rclpy.experimental.async_client. Use this for specialized asynchronous workflows requiring the experimental API.In rclpy, a Service Server is used to provide a service implementation that responds to incoming requests. You can use the standard service or the experimental async service.
rclpy.service. Use this for standard service provider implementations.rclpy.experimental.async_service. Use this for specialized asynchronous service provider implementations.You can build the rclpy documentation locally using Sphinx. This requires an existing ROS 2 installation and specific Python Sphinx dependencies.
Install the required Sphinx extensions via apt:
sudo apt install \
python3-sphinx \
python3-sphinx-autodoc-typehints \
python3-sphinx-rtd-themeSource your ROS 2 installation and build the rclpy package using colcon:
# Source ROS 2 (example for rolling)
. /opt/ros/rolling/setup.bash
# Create workspace and clone source
mkdir -p rclpy_ws/src
cd rclpy_ws/src
git clone https://github.com/ros2/rclpy.git
cd ..
# Build the package
colcon build --symlink-installSource the newly built workspace and use make within the documentation directory:
source install/setup.bash
cd src/rclpy/rclpy/docs
make htmlFor learning how to use rclpy, you can access tutorials, examples, and the full API reference through the official ROS 2 documentation sites:
rclpy on index.ros.orgIn rclpy, you should not instantiate Client or BaseClient directly. Instead, use the create_client method provided by a Node or AsyncNode instance. This ensures the client is correctly managed within the node's lifecycle and context.
Example usage pattern:
# Assuming 'node' is an instance of rclpy.node.Node
client = node.create_client(MySrvType, 'service_name')rclpy.experimental.async_subscription module. This experimental feature allows for asynchronous handling of incoming messages, which can be useful for integrating with async/await patterns in Python.rclpy.subscription module to create subscriptions that receive messages from ROS 2 topics. This module provides the standard mechanism for registering callback functions that are triggered whenever a new message arrives on the subscribed topic.rclpy.experimental.async_publisher module. This is an experimental feature designed to handle publishing operations without blocking the execution flow of the node.rclpy.publisher module to create publishers that send messages over ROS 2 topics. This module provides the standard synchronous publishing interface used in most ROS 2 applications.