#Async closure not working properly with smol?

15 messages · Page 1 of 1 (latest)

safe saffron
#

Have you tried writing smol::spawn(async { ... })?

spiral tiger
safe saffron
#

My apologies, I made a typo

spiral tiger
#

oh wait without the closure oki lemme see

#

it compiles! but it seems that the second spawn is definitely delayed from the first

safe saffron
#

Yes, since you're awaiting them in series

spiral tiger
#

oh i see catponder how do i start them at the same time

safe saffron
# spiral tiger oh i see <:catponder:815088832483164210> how do i start them at the same time

I believe futures::join!() is the tool for this:

use std::time::{Duration, Instant};

fn main() -> anyhow::Result<()> {
    smol::block_on(async {
        let task1 = async {
            for x in 0..5 {
                smol::Timer::after(Duration::from_secs(1)).await;
                println!("{}", x + 1);
            }
        };

        let task2 = async {
            smol::Timer::at(Instant::now() + Duration::from_secs(5)).await;
            println!("5 seconds have passed!");
            smol::Timer::at(Instant::now() + Duration::from_secs(10)).await;
            println!("10 seconds have passed!");
        };

        futures::join!(task1, task2);
        Ok(())
    })
}
#

I'm honestly not quite sure what smol::spawn() is supposed to do

tidal maple
#

probably the same thing as tokio::spawn?

spiral tiger
#

yes

safe saffron
#

Ah, so an async thread::spawn(), that makes sense

#

But futures::join!() should still be the ticket here