Created
March 2, 2017 19:03
-
-
Save hjr3/bdd5b18d9f2e2ab21fac029f246e5c10 to your computer and use it in GitHub Desktop.
MSPC example that doesn't use wait
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[macro_use] extern crate log; | |
extern crate env_logger; | |
extern crate futures; | |
extern crate tokio_core; | |
use std::{thread, time}; | |
use futures::{Stream, Sink, Future}; | |
use futures::sync::mpsc; | |
use tokio_core::reactor::Core; | |
#[derive(Debug)] | |
enum ResponseResult { | |
Success, | |
Failure | |
} | |
#[derive(Debug)] | |
struct Stats { | |
pub success: usize, | |
pub failure: usize, | |
} | |
fn main() { | |
let mut core = Core::new().unwrap(); | |
let remote = core.remote(); | |
let (tx, rx) = mpsc::channel(1); | |
thread::spawn(move || { | |
loop { | |
let tx = tx.clone(); | |
let delay = time::Duration::from_secs(1); | |
thread::sleep(delay); | |
remote.spawn(|handle| { | |
handle.spawn( | |
tx | |
.send(ResponseResult::Success) | |
.then(|_| Ok(())) // spawn expects Ok(()) | |
); | |
Ok(()) | |
}); | |
} | |
}); | |
let mut stats = Stats { success: 0, failure: 0 }; | |
let f = rx.for_each(|res| { | |
match res { | |
ResponseResult::Success => stats.success += 1, | |
ResponseResult::Failure => stats.failure += 1, | |
} | |
println!("stats = {:?}", stats); | |
Ok(()) | |
}); | |
core.run(f).expect("Core failed to run"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment