spawn

Function spawn 

Source
pub fn spawn(future: impl Future<Output = ()> + 'static)
Available on crate feature async-spawn only.
Expand description

Spawn the provided future to get executed concurrently with the currently-running async computation.

This API is somewhat similar to tokio::task::spawn for example but has a number of limitations to be aware of. If possible it’s recommended to avoid this, but it can be convenient if these limitations do not apply to you:

  • Spawned tasks do not work when the version of the wit-bindgen crate managing the export bindings is different from the version of this crate. To work correctly the spawn function and export executor must be at exactly the same version. Given the major-version-breaking nature of wit-bindgen this is not always easy to rely on. This is tracked in #1305.

  • Spawned tasks do not outlive the scope of the async computation they are spawned within. For example with an async export function spawned tasks will be polled within the context of that component-model async task. For computations executing within a block_on call, however, the spawned tasks will be executed within that scope. This notably means that for block_on spawned tasks will prevent the block_on function from returning, even if a value is available to return.

  • There is no handle returned to the spawned task meaning that it cannot be cancelled or monitored.

  • The task spawned here is executed concurrently, not in parallel. This means that while one future is being polled no other future can be polled at the same time. This is similar to a single-thread executor in Tokio.

With these restrictions in mind this can be used to express execution-after-returning in the component model. For example once an exported async function has produced a value this can be used to continue to execute some more code before the component model async task exits.