[][src]Function async_runtime::spawn

pub fn spawn(
    fut: impl Future<Output = ()> + 'static + Send
) -> Result<(), Error>

Spawn a future to be run on the thread specified executor (set with init).

This method returns a result. I understand that this is an inconveniece, but this is a interface that abstracts out over all supported executors. Some of them don't have an infallible spawn method, so we return a result even though on most executors spawning is infallible.

Most of the time failing to spawn is rather fatal, so often using "expect" is fine. In application code you will know which executor you use, so you know if it's fallible, but library authors need to take into account that this might be fallible and consider how to recover from it.

Note that localpool and bindgen will box the future.

spawn requires a Send bound on the future. See spawn_local if you have to spasn !Send futures.

spawn requires a () Output on the future. If you need to wait for the future to finish and/or recover a result from the computation, see: spawn_handle.

Errors

  • This method is infallible on: juliex, async-std, bindgen.
  • On the localpool executor, this method can return a ErrorKind::Spawn if the executor has been shut down. See the docs for the futures library. I haven't really found a way to trigger this error. You can call [localpool::run] and spawn again afterwards.
  • If you call this without an initialized executor, ErrorKind::NoExecutorInitialized is returned.

Example