Created
May 4, 2020 20:53
-
-
Save jamesmcm/31f2283c28acfbfccf42489f4db528e2 to your computer and use it in GitHub Desktop.
tokio_synchronous.rs
This file contains 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 std::error::Error; | |
use std::time::{Duration, Instant}; | |
use tokio::net::TcpStream; | |
use tokio::prelude::*; | |
#[tokio::main] | |
async fn main() -> Result<(), Box<dyn Error>> { | |
let now = Instant::now(); | |
task("task1", &now).await?; | |
task("task2", &now).await?; | |
task("task3", &now).await?; | |
Ok(()) | |
} | |
async fn task(label: &str, now: &std::time::Instant) -> Result<(), Box<dyn Error>> { | |
println!("{} started: {:?}", label, now.elapsed()); | |
tokio::time::delay_for(tokio::time::Duration::from_secs(2)).await; | |
let mut stream = TcpStream::connect("127.0.0.1:6142").await?; | |
stream.write_all(label.as_bytes()).await?; | |
println!("{} written: {:?}", label, now.elapsed()); | |
let mut buffer = [0; 5]; | |
stream.read_exact(&mut buffer).await?; | |
stream.shutdown(std::net::Shutdown::Both)?; | |
println!( | |
"{} finished: {:?}", | |
std::str::from_utf8(&buffer)?, | |
now.elapsed() | |
); | |
Ok(()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment