Ladies and Gentleman,
I would like to spawn asynchronous futures that store their results in a mutex provided by futures-locks crate. I can process the futures asynchronously but saving the result to a mutex is my difficulty. The following MCVE involves my strategy of running fut1 and somehow ™ passing the result to fut2 where it can be stored in mutex but is this an idiomatic approach for storing async processes into a mutex? Thank you most excellent Rust community!
extern crate futures;
extern crate futures_locks;
extern crate tokio;
extern crate tokio_process;
use futures::Future;
use std::process::Command;
use std::time::Duration;
use tokio::prelude::*;
use tokio_process::CommandExt;
fn main() {
let cmds = vec!["users.rust-lang.org", "www.reddit.com", "youtube.com"]
.iter()
.map(|x| vec![format!("ping -c 2 {}", x), format!("dig {}", x)].join(";"))
.collect::<Vec<_>>();
let mutex = futures_locks::Mutex::<Vec<_>>::new(vec![]);
let clone0 = mutex.clone();
let parent = stream::iter_ok(cmds).for_each(move |cmd| {
let fut1 = Command::new("bash")
.arg("-c")
.arg(&cmd)
.output_async()
.timeout(Duration::from_secs(3))
.map_err(|e| println!("failed to collect output: {}", e))
.map(|i| {
// Pass this value to fut2 somehow?
// Is this idiomatic strategy?
dbg!(i);
});
// fut2 can save to mutex
let fut2 = clone0.lock().map(move |mut guard| guard.push(cmd));
tokio::spawn(fut1);
tokio::spawn(fut2);
future::ok(())
});
tokio::run(parent);
// Need result of fut1 available here
dbg!(mutex.try_unwrap().unwrap());
}