2
0
Fork 0
mirror of https://git.asonix.dog/asonix/pict-rs synced 2024-12-22 19:31:35 +00:00

Improve cooperation from from_iterator to prevent task starvation

This commit is contained in:
asonix 2024-01-05 19:50:10 -06:00
parent 4145637a33
commit bcf73eb4e4

View file

@ -76,10 +76,22 @@ where
streem::from_fn(|yielder| async move { streem::from_fn(|yielder| async move {
let mut stream = rx.into_stream().into_streamer(); let mut stream = rx.into_stream().into_streamer();
let yield_count = buffer.max(8);
let mut count = 0;
while let Some(res) = stream.next().await { while let Some(res) = stream.next().await {
tracing::trace!("from_iterator: looping"); tracing::trace!("from_iterator: looping");
count += 1;
count = count % yield_count;
yielder.yield_(res).await; yielder.yield_(res).await;
// every 8 (or buffer-size) items, yield to executor before looping
// improves cooperation
if count == 0 {
tokio::task::yield_now().await;
}
} }
let _ = handle.await; let _ = handle.await;