Skip to main content

Module spawn

Module spawn 

Source
Expand description

The pluggable executor async exports run on, and the launcher driver that keeps the completion promise.

An exported async fn lowers to a launcher that returns immediately and a completion callback that fires when the future resolves. Something has to drive the future in between:

  • By default, a fixed pool of worker threads (one per available core, at least two) started lazily on the first launch. Workers share one run queue, and a future’s waker puts it back on the queue, so a launch never costs a thread. The pool has no reactor, and its workers are few: a future that blocks a worker for a long time (synchronous I/O, a long computation) holds up every other call, so such work belongs on a runtime of its own (the tokio feature, or set_spawner).
  • With the tokio feature, a Tokio runtime: the current one when the launcher is called from inside a runtime, otherwise a multi-thread runtime the library creates on first use.
  • On wasm32, which has no threads, the future is polled inline before the launcher returns. A future still pending once nothing wakes it completes with GENERIC_ERROR_CODE (“async function suspended with no executor on wasm32”) instead of spinning.

A producer installs its own Spawner once at startup with set_spawner, which overrides every default:

// e.g. `|fut| { tokio_handle.spawn(fut); }`
let _ = weaveffi::abi::set_spawner(|fut| spawn_on_my_runtime(fut));

Generated launchers call launch_async, which runs the launcher’s own work (lifting the inputs) under catch_unwind and hands the producer’s future to run_async. Every future handed to a spawner is Send + 'static and already wrapped so a panic inside it is caught and reported through the completion callback; a spawner never observes an unwinding future. A spawner may also drop a future without finishing it (a runtime shutting down, say): the completion still fires, with CANCELLED_ERROR_CODE. A spawner that panics, or a default executor that can’t start, completes the call with GENERIC_ERROR_CODE.

Structs§

CatchUnwind
A future adapter that catches a panic from the inner future’s poll and resolves to Err(payload) instead of unwinding into the executor.
SpawnerAlreadySet
The error returned when set_spawner is called a second time.

Enums§

SpawnError
Why the executor didn’t take a future.

Traits§

Spawner
An executor hook that drives the futures produced by async exports.

Functions§

block_on
Drive a future to completion on the current thread, blocking until it resolves.
default_pool_size
The number of worker threads the default executor runs: one per available core, at least two. (With the tokio feature or on wasm32 the default executor is something else, and this is 0.)
launch_async
The body of every generated async launcher.
run_async
Run an async export to completion and deliver its result exactly once.
set_spawner
Install the process-wide spawner async exports run on.
spawn
Run fut on the installed spawner, or on the default executor.

Type Aliases§

BoxFuture
The type-erased future a Spawner receives.