Tokio: Better error from tokio::spawn() if no `Runtime` is running

Created on 30 May 2018  路  4Comments  路  Source: tokio-rs/tokio

Currently if you run a current_thread::Executor, running spawn() from the futures run by that executor will fail with SpawnError { is_shutdown: true }. It would be nice if the error was clearer here about what's going wrong. (For more context see #377.)

Most helpful comment

This is correct. If you need to call tokio::spawn, you should wrap the init code w/ lazy and pass that to tokio::run. Something like:

tokio::run(future::lazy(|| {
   tokio::spawn(my_task());
   Ok(())
}))

I want to make this nicer in 0.2

All 4 comments

Do I understand correctly that this means I can't call tokio::spawn before calling tokio::run?

This is correct. If you need to call tokio::spawn, you should wrap the init code w/ lazy and pass that to tokio::run. Something like:

tokio::run(future::lazy(|| {
   tokio::spawn(my_task());
   Ok(())
}))

I want to make this nicer in 0.2

Cool, that solved my problem for now.

The message is now: "must be called from the context of Tokio runtime"

Was this page helpful?
0 / 5 - 0 ratings