| Crates.io | adirector |
| lib.rs | adirector |
| version | 0.1.1 |
| created_at | 2024-03-09 17:59:26.117175+00 |
| updated_at | 2024-03-09 18:09:04.106793+00 |
| description | asynchronous tokio task spawner with a limited size |
| homepage | |
| repository | https://github.com/redcloudvg/adirector |
| max_upload_size | |
| id | 1168029 |
| size | 41,239 |
asynchronous tokio task spawner with a limited size.
Full example:
use adirector::{Director, DirectorError};
#[tokio: main]
async fn main() -> Result<(), DirectorError> {
// create executor that allow 10 tasks concurrently.
let mut director = Director::new(10);
// read line by line stdin
let mut lines = BufReader::new(stdin()).lines();
while let Some(line) = lines.next_line().await.unwrap() {
director.spawn(async move {
println!("{}", line);
sleep(Duration::from_millis(50)).await;
}).await?; // Suspends until the task is spawned
}
// Wait for remaining tasks to complete
director.join_all().await
}
A Semaphore is used to limit the count of tasks, and a JoinSet to join all tasks at once.
rt sync features