Skip to main content

spawn_task

Function spawn_task 

Source
pub fn spawn_task<T, W, F>(work: W, on_done: F) -> Task
where T: Send + 'static, W: FnOnce() -> T + Send + 'static, F: FnOnce(T) + 'static,
Expand description

Runs work on a background thread and on_done with its result on this thread, during a later frame’s drain_tasks.

This is the supported way to get a background result into a signal: on_done stays here, so it may close over !Send state and write signals directly, while work and the value it produces cross the thread boundary and must be Send.

spawn_task(
    || expensive_query(),          // worker thread
    move |rows| results.set(rows), // UI thread, a later frame
);

Work runs on a pooled thread that may block freely — the pool grows rather than starve. A panic inside work abandons the task: on_done is dropped without running.