DEV Community

toyster
toyster

Posted on

Using async runtimes together

Using both Tokio and async-std together

Option 1 using async-compat

// async-compat = "0.2.1"
// async-std = {version = "1.10.0"}
// tokio = { version = "1", features = ["full"] }
use async_compat::Compat;

async fn say_hello() {
    println!("Hello, world!");
}

fn main() {
    async_std::task::block_on(Compat::new(async {
   tokio::spawn(async { 
       say_hello().await; // some async function
       println!("this is also hello")
   });
    }));
}
Enter fullscreen mode Exit fullscreen mode

Option 2 using async-std's tokio1 runtime

// async-std = {version = "1.10.0", features = ["attributes", "tokio1"]}
// tokio = { version = "1", features = ["full"] }

async fn say_hello() {
    println!("Hello, world!");
}

#[async_std::main]
async fn main() {
    let t = tokio::spawn(async {
   say_hello().await; // some async function
   println!("this is also hello")
    });
    t.await.unwrap()
}
Enter fullscreen mode Exit fullscreen mode

Discussion (0)