Skip to content

Instantly share code, notes, and snippets.

@jamesmcm
Created May 4, 2020 20:53
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 jamesmcm/31f2283c28acfbfccf42489f4db528e2 to your computer and use it in GitHub Desktop.
Save jamesmcm/31f2283c28acfbfccf42489f4db528e2 to your computer and use it in GitHub Desktop.
tokio_synchronous.rs
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