12 releases
0.9.3 | Nov 1, 2024 |
---|---|
0.9.1 | Dec 4, 2023 |
0.8.1 | Oct 25, 2023 |
0.5.1 | Jul 10, 2023 |
0.5.0 | Nov 25, 2022 |
#1668 in Asynchronous
273 downloads per month
Used in rust-box
115KB
3K
SLoC
Task Execution Queue
English | 简体中文
Task Execution Queue A task execution queue. Can limit the number of concurrent tasks and execution order of the same type of tasks can be controlled. Generally, asynchronous tasks can be executed directly using Tokio or async-std; However, in some special business scenarios, when we need to execute tasks in batches and control the concurrent number of tasks, using spawn() directly can easily lead to excessive load and exhaustion of resources such as CPU or memory; This Crate is developed to solve such problems.
Features
- Execute the tasks;
- Execute the tasks and return results;
- Limit the number of concurrent tasks
- Task queue;
- Sequential execution of the same type of tasks;
- Support Local tasks
Plan
Examples
- 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);
}
- sequential execution
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).group().build::<&str>();
let root_fut = async move {
spawn(async {
//start executor
task_runner.await;
});
//execute task ...
let _res = async move {
println!("hello world!");
}.spawn(&exec).group("g1").await;
let res = async move {
"hello world!"
}.spawn(&exec).group("g1").result().await;
println!("result: {:?}", res.ok());
exec.flush().await;
println!("exec.actives: {}, waitings: {}, completeds: {}", exec.active_count(), exec.waiting_count(), exec.completed_count());
};
async_std::task::block_on(root_fut);
}
More Examples
Dependencies
~3.5–9MB
~85K SLoC