< 返回版块

rust-box 发表于 2022-10-06 22:42

Tags:asynchronous,concurrency,task

Crates

  • queue-ext 队列的扩展特征,可将队列的pop()函数转换为Stream,push()函数转换为Sink。
  • task-exec-queue 一个任务执行队列。可限制任务并发执行数量,可控制同一类任务执行顺序。

使用

加入下面依赖到你的 Cargo.toml:

[dependencies]
rust-box = "0.4"

例子

  • quick start
fn main() {
    use async_std::task::spawn;
    use rust_box::task_exec_queue::{init_default, default, SpawnDefaultExt};

    let task_runner = init_default();
    let root_fut = async move {
        spawn(async {
            //start executor
            task_runner.await;
        });

        //execute task ...
        let _ = async {
            println!("hello world!");
        }.spawn().await;

        default().flush().await;
    };
    async_std::task::block_on(root_fut);
}

  • execute and return result
fn main() {
    use async_std::task::spawn;
    use rust_box::task_exec_queue::{Builder, SpawnExt};
    let (exec, task_runner) = Builder::default().workers(10).queue_max(100).build();
    let root_fut = async move {
        spawn(async {
            //start executor
            task_runner.await;
        });

        //execute task and return result...
        let res = async {
            "hello world!"
        }.spawn(&exec).result().await;
        println!("result: {:?}", res.ok());

        exec.flush().await;
    };
    async_std::task::block_on(root_fut);
}

更多例子


Ext Link: https://github.com/try-box/rust-box

评论区

写评论

还没有评论

1 共 0 条评论, 1 页