Skip to content

Instantly share code, notes, and snippets.

@0xNineteen
Created March 6, 2023 15:12
Show Gist options
  • Save 0xNineteen/3b9a60107447821c57cfc31febc1b012 to your computer and use it in GitHub Desktop.
Save 0xNineteen/3b9a60107447821c57cfc31febc1b012 to your computer and use it in GitHub Desktop.
helloworld
use std::error::Error;
use std::time::Duration;
use tokio::sync::mpsc::{channel, Receiver, Sender};
use tokio::time::sleep;
use tokio::runtime::Builder;
use tracing_subscriber;
use tracing::{info, Instrument};
pub async fn generator(sender: Sender<u64>) {
let mut i = 0;
loop {
info!("sending: {i}");
sender.send(i).await.unwrap();
sleep(Duration::from_secs(1)).await;
i += 1;
}
}
pub async fn processor(mut reciever: Receiver<u64>) {
loop {
let result = reciever.recv().await.unwrap();
info!("recieved: {result}");
}
}
fn main() -> Result<(), Box<dyn Error>>{
tracing_subscriber::fmt::init();
let runtime = Builder::new_multi_thread().enable_all().build().unwrap();
runtime.block_on(async move {
let (sender, reciever) = channel(100);
tokio::spawn(
generator(sender)
.instrument(tracing::info_span!("sender"))
);
tokio::spawn(
processor(reciever)
.instrument(tracing::info_span!("reciever"))
);
loop { }
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment