Hi! I am trying to learn about using tokio. But now I am stuck in understanding spawning and tasks. What exactly is a task and how is it different from a future? The docs mention that the tasks are like "asynchronous goroutines"? But the analogu doesn't make any sense to me. Please hep me here.
And the second one is the lazy function. According to the docs,
Creates a new future which will eventually be the same as the one created by the closure provided.
The provided closure is only run once the future has a callback scheduled on it, otherwise the callback never runs. Once run, however, this future is the same as the one the closure creates.
What does callback refer to here? What is meant by the "future from the closure" and "future created from the lazy function"? What is meant by them eventually becoming one?
Please explain this with some example.
tokio::run(lazy(|| {
let (tx, rx) = oneshot::channel();
tokio::spawn(lazy(|| {
tx.send("hello from spawned task");
Ok(())
}));
rx.and_then(|msg| {
println!("Got `{}`", msg);
Ok(())
})
.map_err(|e| println!("error = {:?}", e))
}));
Here in the above example what is the use of Ok(())? Shouldn't it be something like Ok(Async::Result(())), if the closure gives rise to the future?
A reddit post for the same.
https://www.reddit.com/r/rust/comments/boew9z/a_few_doubts_on_using_tokio/?utm_source=reddit-android