Iced + Tokio Spawn - Unable to return values from async closure
⚓ Rust 📅 2026-04-06 👤 surdeus 👁️ 6I am attempting to learn how to run multiple tasks in parallel. I am using Tokio to do so. I am able to spawn tasks and obtain the return values within the associated closure. But I am unable to determine how to access these values outside the closure. The goal is to be able to assign the 3 vectors returned from the 3 tasks to state variables and then change the screen.
Any assistance will be appreciated. Below is the code which is in the update function of an iced program.
Message::DatabaseUpdated(rec_read, rec_updated, rec_inserted) => {
println!("Entered DatabaseUpdated message handler...");
self.records_read = rec_read;
self.records_updated = rec_updated;
self.records_inserted = rec_inserted;
let start_date: String = get_start_date(self.time_interval).unwrap();
let task1 = async {
get_all_close(start_date)
};
let task2 = async {
get_close(QueryType::Min)
};
let task3 = async{
get_close(QueryType::Max)
};
println!("Beginning tokio spawn for async tasks...");
tokio::spawn(async {
let handle1 = tokio::spawn(task1);
let handle2 = tokio::spawn(task2);
let handle3 = tokio::spawn(task3);
let results = tokio::join!(handle1, handle2, handle3);
// let results: (
// Result<impl Future<Output = Result<Vec<QueryResults>, IoSqlError», JoinError>,
// Result<impl Future<Output = Result<Vec<QueryResults>, IoSqlError»>, JoinError>,
// Result<impl Future<Output = Result<Vec<QueryResults>, IoSqlError>>, JoinError>)
let selected_values = results.0.unwrap().await.unwrap();
let min_values = results.1.unwrap().await.unwrap();
let max_values = results.2.unwrap().await.unwrap();
println!("Selected values count : {}", selected_values.len());
println!("Min values count : {}", min_values.len());
println!("Max values count : {}", max_values.len());
println!("Async tasks completed. ...................");
});
println!("Async tasks spawned. Waiting for completion...");
println!("Leaving DatabaseUpdated message handler...");
Task::none()
} // end of Message::DatabaseUpdated
4 posts - 2 participants
🏷️ Rust_feed