pub fn work_queue<W, K, C>(
concurrency: usize,
work_fn: W,
) -> (WorkQueue<W, K, C>, WorkSender<K, C, <W::Future as Future>::Output>)
Expand description
Creates an unbounded queue of work tasks that will execute up to concurrency
worker
s at once.
ยงExamples
#[derive(Debug, Clone)]
enum DownloadError {}
async fn download_file(url: String, _context: ()) -> Result<Vec<u8>, DownloadError> {
// ...
}
let mut executor = futures::executor::LocalPool::new();
executor.run_until(async move {
let (mut processor, sender) = work_queue(2, download_file);
let mut join_handles = vec![];
for crate_name in vec!["rand", "lazy_static", "serde", "regex"] {
let fut = sender.push(format!("https://crates.io/api/v1/crates/{}", crate_name), ());
join_handles.push((crate_name, fut));
}
// The queue stream won't terminate until all sender clones are dropped.
drop(sender);
while let Some(key) = processor.next().await {
println!("Finished processing {}", key);
}
for (crate_name, fut) in join_handles {
let res = fut
.await
.expect("queue to execute the task")
.expect("downloads can't fail, right?");
println!("Contents of {}: {:?}", crate_name, res);
}
});