Function work_queue::work_queue

source ·
pub fn work_queue<W, K, C>(
    concurrency: usize,
    work_fn: W
) -> (WorkQueue<W, K, C>, WorkSender<K, C, <W::Future as Future>::Output>)where
    W: Work<K, C>,
    K: Clone + Eq + Hash,
    C: TryMerge,
Expand description

Creates an unbounded queue of work tasks that will execute up to concurrency workers at once.


#[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!("{}", crate_name), ());
        join_handles.push((crate_name, fut));

    // The queue stream won't terminate until all sender clones are dropped.

    while let Some(key) = {
        println!("Finished processing {}", key);

    for (crate_name, fut) in join_handles {
        let res = fut
            .expect("queue to execute the task")
            .expect("downloads can't fail, right?");
        println!("Contents of {}: {:?}", crate_name, res);