Created
September 26, 2019 09:47
-
-
Save osa1/6e113138f860dd6d773f23de9a2a2635 to your computer and use it in GitHub Desktop.
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
[package] | |
name = "blocking_test" | |
version = "0.1.0" | |
authors = ["Ömer Sinan Ağacan <[email protected]>"] | |
edition = "2018" | |
[dependencies] | |
futures-preview = { version = "0.3.0-alpha.18", features = ["async-await", "nightly"] } | |
futures-util-preview = "0.3.0-alpha.18" | |
tokio = { git = "https://github.com/tokio-rs/tokio.git", features = ["timer"] } | |
tokio-executor = { git = "https://github.com/tokio-rs/tokio.git" } |
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
use futures::future::FutureExt; | |
use futures::stream::StreamExt; | |
use futures::{pin_mut, select}; | |
use std::net::{SocketAddr, ToSocketAddrs}; | |
async fn dns_task() -> Option<impl Iterator<Item = SocketAddr>> { | |
match tokio_executor::blocking::run(move || { | |
std::thread::sleep(std::time::Duration::from_secs(5)); | |
("basdfasfdasdf.net", 1234).to_socket_addrs() | |
}) | |
.await | |
{ | |
Err(io_err) => { | |
println!("Address name resolve failed: {:?}", io_err); | |
None | |
} | |
Ok(addr_iter) => Some(addr_iter), | |
} | |
} | |
fn main() { | |
let mut executor = tokio::runtime::current_thread::Runtime::new().unwrap(); | |
let (mut snd_stop, rcv_stop) = tokio::sync::mpsc::channel::<()>(1); | |
let mut rcv_stop_fused = rcv_stop.fuse(); | |
executor.spawn(async move { | |
let dns_task = dns_task().fuse(); | |
pin_mut!(dns_task); | |
select! { | |
addr_iter = dns_task => { | |
match addr_iter { | |
None => { | |
println!("Can't resolve addr name"); | |
return; | |
} | |
Some(addr_iter) => { | |
println!("Address name resolved:"); | |
for addr in addr_iter { | |
println!("{:?}", addr); | |
} | |
} | |
} | |
}, | |
_ = rcv_stop_fused.next() => { | |
return; | |
} | |
} | |
}); | |
executor.spawn(async move { | |
tokio::timer::delay_for(std::time::Duration::from_secs(3)).await; | |
snd_stop.send(()).await.unwrap(); | |
}); | |
executor.run().unwrap(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment