Skip to content

Instantly share code, notes, and snippets.

@zbentley
Created February 5, 2024 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zbentley/d05696b1f6268372cf858b157c6d7740 to your computer and use it in GitHub Desktop.
Save zbentley/d05696b1f6268372cf858b157c6d7740 to your computer and use it in GitHub Desktop.
Rust Heterogenous Select
use tokio::io::AsyncReadExt;
use tokio::net::TcpStream;
use tokio::select;
use tokio::task::spawn_blocking;
use tokio::time::{sleep, Duration};
async fn main() {
let mut stream = TcpStream::connect("...").await?;
let thread_handle = spawn_blocking(|| {
do_something();
});
select! {
_ = stream.read(buf) => (),
_ = sleep(Duration::from_secs(1)) => (),
_ = thread_handle => ()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment