Created
March 16, 2022 07:32
-
-
Save jerry73204/212148bd481d5cf4e6a95cfb6c58e530 to your computer and use it in GitHub Desktop.
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 futures::future::FutureExt as _; | |
use futures::stream::StreamExt as _; | |
use futures::stream; | |
use futures::join; | |
use std::task::Poll; | |
use std::time::Duration; | |
use std::time::Instant; | |
async fn pending_futures() { | |
let (tx, rx) = flume::bounded(4); | |
let consumer_future = consumer(rx); | |
let stream = stream::repeat(()); | |
let forward_future = stream.map(Ok).forward(tx.into_sink()); | |
let idle_pending_future = futures::future::poll_fn(|ctx| -> Poll<()> { | |
Poll::Pending | |
}); | |
let busy_pending_future = futures::future::poll_fn(|ctx| -> Poll<()> { | |
ctx.waker().wake_by_ref(); | |
dbg!(Instant::now()); | |
Poll::Pending | |
}); | |
} | |
async fn async_std_blocking() { | |
let futures = (0..12).map(|_| { | |
async_std::task::spawn(async move { | |
block(); | |
}) | |
}); | |
let fut = async_std::task::spawn(async move { | |
loop { | |
async_std::task::sleep(Duration::from_millis(100)).await; | |
println!("i'm alive!"); | |
} | |
}); | |
join!( futures::future::join_all( futures), fut); | |
} | |
async fn consumer(rx: flume::Receiver<()>) { | |
rx.into_stream().for_each(|()| async move { | |
println!("do nothing"); | |
}).await; | |
} | |
fn block() { | |
loop { | |
eprintln!(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment