mirror of
https://git.asonix.dog/asonix/pict-rs
synced 2024-12-22 11:21:24 +00:00
Improve cooperation from from_iterator to prevent task starvation
This commit is contained in:
parent
4145637a33
commit
bcf73eb4e4
1 changed files with 12 additions and 0 deletions
|
@ -76,10 +76,22 @@ where
|
|||
streem::from_fn(|yielder| async move {
|
||||
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 {
|
||||
tracing::trace!("from_iterator: looping");
|
||||
|
||||
count += 1;
|
||||
count = count % yield_count;
|
||||
|
||||
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;
|
||||
|
|
Loading…
Reference in a new issue