Replies: 1 comment 2 replies
-
|
The "basic executor" provided by rclrs doesn't use tokio, so If you need to spawn tasks using tokio---some Rust libraries have functions that can only be run when backed by a tokio runtime, so this might not be avoidable---then it would probably be a good idea to implement a tokio-based executor as an alternative to the basic executor. Then your async subscription callbacks will automatically be spawned as tokio tasks. If implementing a tokio-based executor is too daunting, another approach you could take is to use a regular (non-async) subscription and just pass it a sender, while its receiver is used in a tokio-spawned task and listening for new subscriptions to come in. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
code:
fn cmd_topic_handle(&mut self) {
let sub = self
.node
.create_async_subscription::<std_msgs::msg::String, _>(
"/run/cmd",
move |req: std_msgs::msg::String| {
async move {
let _ = tokio::spawn(async move {
logger_info!("receive cmd: {}", &req.data);
})
.await;
}
},
)
.unwrap();
self.topic_subs.push(sub);
}
exec ros2 command:
ros2 topic pub /run/cmd std_msgs/msg/String
use shell command:
top -H -p {pid}
waitting the result:
938271 robot 20 0 1095504 23680 17024 R 99.3 0.1 1:44.65 robot_cmd_exec
938273 robot 20 0 1095504 23680 17024 S 0.0 0.1 0:00.00 tokio-runtime-w
938274 robot 20 0 1095504 23680 17024 S 0.0 0.1 0:00.00 tokio-runtime-w
938275 robot 20 0 1095504 23680 17024 S 0.0 0.1 0:00.00 tokio-runtime-w
938276 robot 20 0 1095504 23680 17024 S 0.0 0.1 0:00.00 tokio-runtime-w
938277 robot 20 0 1095504 23680 17024 S 0.0 0.1 0:00.01 tokio-runtime-w
938278 robot 20 0 1095504 23680 17024 S 0.0 0.1 0:00.02 tokio-runtime-w
938279 robot 20 0 1095504 23680 17024 S 0.0 0.1 0:00.00 robot_cmd_exec
938280 robot 20 0 1095504 23680 17024 S 0.0 0.1 0:00.05 robot_cmd_exec
938281 robot 20 0 1095504 23680 17024 S 0.0 0.1 0:00.01 robot_cmd_exec
938282 robot 20 0 1095504 23680 17024 S 0.0 0.1 0:00.03 robot_cmd_exec
According to actual use measurements, in ROS2-Rust's asynchronous subscription, if "tokio::spawn" is used to asynchronously wait for a task to complete,
when the number of subscription calls reaches a certain threshold (about 130+), it leads to a sudden spike in CPU usage to 100%. Is this an issue with the code library?
Beta Was this translation helpful? Give feedback.
All reactions