Crates.io | queued-task |
lib.rs | queued-task |
version | 0.1.1 |
source | src |
created_at | 2024-08-01 09:54:48.028632 |
updated_at | 2024-08-02 04:58:46.763511 |
description | concurrent queue task processing |
homepage | |
repository | https://github.com/zhengxiwan/queued-task-rs |
max_upload_size | |
id | 1321862 |
size | 8,314 |
This Rust library provides a robust solution for handling queue tasks in high concurrency scenarios. It ensures tasks are processed in order, enhancing service stability. Additionally, it allows configurable parallel task processing to optimize performance.
Add this to your Cargo.toml
:
[dependencies]
queued-task = "0.1.0"
use std::sync::Arc;
use std::time::Duration;
use queued_task::QueuedTaskBuilder;
#[tokio::test]
async fn test() {
// 10 queue_len, 2 process rate
let t = Arc::new(QueuedTaskBuilder::new(10, 2).handle(handle).build());
async fn handle(wait_time: Duration, c: usize) -> usize {
tokio::time::sleep(Duration::from_secs(1)).await;
println!("{} {}", c, wait_time.as_millis());
c
}
let mut ts = vec![];
for i in 0..20 {
let tt = t.clone();
ts.push(tokio::spawn(async move {
// push task
let state = tt.push(i).await.unwrap();
// waiting for task result
let result = state.wait_result().await;
dbg!(result);
}));
}
for x in ts {
let _ = x.await;
}
}