Sprinter 👟
A Rust library for running parallel queued tasks with controlled concurrency.
Features
- ⚡ Run tasks in parallel with configurable concurrency limits
- 🔄 Queue tasks and manage their execution
- 📊 Track task states (Pending, Running, Succeed, Failed)
- 🚦 Control task flow with push/wait mechanisms
- 🔍 Monitor individual task completion and results
Installation
Add this to your Cargo.toml
:
[]
= "0.0.1-dev"
Usage
Basic Example
Here's a basic example of using Sprinter to run parallel tasks:
use Queue;
use Error;
use sleep;
async
It will print:
sprint start ...
task1 start ...
task2 start ...
task2 done!
task3 start ...
task3 done!
task1 done!
sprint done in 251.949651ms
results: {"task1": Ok(Ok(1)), "task2": Ok(Ok(2)), "task3": Ok(Ok(3))}
This will execute tasks in parallel with a maximum concurrency of 2, meaning two tasks can run simultaneously. The output will show tasks running and completing based on their duration and the concurrency limit.
API
Creating a Queue
let queue: = new;
concurrency
: Maximum number of tasks that can run in paralleltick_interval
: Interval in milliseconds between queue processing ticksT
: Type of successful task resultE
: Type of task error
Methods
push
async
Pushes a new task to the queue, it will be executed as soon as it is pushed or when the queue is ready. Each task must have a unique ID, tasks execution is deduped by task_id.
set_push_done
async
Signals that all tasks have been pushed to the queue. Note the queue will not complete until set_push_done
is called, even if all tasks have been completed.
task_done
async
Waits for a specific task to complete and returns its result.
wait_for_tasks_done
async
Waits for all tasks to complete and returns a map of task IDs to their results. set_push_done
must be called before this method.
reset
async
Resets the queue to its initial state, clearing all tasks and results.
Task States
Tasks can be in one of these states:
Pending
: Task is queued but not yet runningRunning
: Task is currently executingSucceed
: Task completed successfullyFailed
: Task failed with an error
License
This project is licensed under the MIT License - see the LICENSE file for details.