Skip to content

[Question] Any problems with running rclrs through tokio? #620

@drewlwhitney

Description

@drewlwhitney

I've found that I can basically do

use example_interfaces::msg::Bool;
use rclrs::*;
use std::time::Duration;

#[tokio::main]
async fn main() {
    let mut executor = Context::default_from_env().unwrap().create_basic_executor();
    let node = executor.create_node("test").unwrap();

    let executor_task = tokio::task::spawn_blocking(move || executor.spin(SpinOptions::default()));

    node.create_async_subscription("topic_foo", async |msg: Bool| {
        println!("one");
        tokio::time::sleep(Duration::from_secs(1)).await;
        println!("two");
        tokio::time::sleep(Duration::from_secs(1)).await;
        println!("three");
    });

    executor_task.await.unwrap().first_error().unwrap();
}

and this works (although weirdly, using tokio::task::spawn() with executor.run_async() causes a crash). Am I going to run into issues doing this? I really like having some of tokio's tools at my disposal, and I really want to use an async fn main().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions