pub fn respond<R, F>(task: F) -> ReadOnlyVar<Response<R>, ArcVar<Response<R>>>
Expand description
Spawn a parallel async task that will send its result to a ResponseVar<R>
.
The run
documentation explains how task
is parallel and async. The task
starts executing immediately.
§Examples
fn on_event(&mut self) {
self.sum_response = task::respond(async {
read_numbers().await.par_iter().map(|i| i * i).sum()
});
}
fn on_update(&mut self) {
if let Some(result) = self.sum_response.rsp_new() {
println!("sum of squares: {result}");
}
}
The example .await
for some numbers and then uses a parallel iterator to compute a result. The result is send to
sum_response
that is a ResponseVar<R>
.
§Cancellation
Dropping the ResponseVar<R>
does not cancel the task
, it will still run to completion.
§Panic Handling
If the task
panics the panic is logged as an error and resumed in the response var modify closure.