Crates.io | task-exec-queue |
lib.rs | task-exec-queue |
version | 0.9.3 |
source | src |
created_at | 2022-10-06 09:44:29.327448 |
updated_at | 2024-11-01 15:27:48.220899 |
description | A asynchronous task execution queue |
homepage | https://github.com/try-box/rust-box/tree/main/task-exec-queue |
repository | https://github.com/try-box/rust-box/tree/main/task-exec-queue |
max_upload_size | |
id | 681140 |
size | 82,446 |
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.
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);
}
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);
}
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);
}