pub struct PriorityTaskQueue { /* private fields */ }Expand description
Priority task queue with weighted scheduling
Tasks are dequeued based on their priority weights. Higher priority tasks have a higher chance of being selected, but lower priority tasks are not starved due to the weighted scheduling algorithm.
§Example
use reinhardt_tasks::{Priority, PriorityTaskQueue};
let queue = PriorityTaskQueue::new();
// High priority tasks are more likely to be dequeued first
// but low priority tasks will also be processedImplementations§
Source§impl PriorityTaskQueue
impl PriorityTaskQueue
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new priority task queue with default weights
Default weights:
- High: 100
- Normal: 50
- Low: 10
§Example
use reinhardt_tasks::PriorityTaskQueue;
let queue = PriorityTaskQueue::new();Sourcepub fn with_weights(weights: HashMap<Priority, u32>) -> Self
pub fn with_weights(weights: HashMap<Priority, u32>) -> Self
Create a new priority task queue with custom weights
§Example
use reinhardt_tasks::{Priority, PriorityTaskQueue};
use std::collections::HashMap;
let mut weights = HashMap::new();
weights.insert(Priority::High, 200);
weights.insert(Priority::Normal, 100);
weights.insert(Priority::Low, 20);
let queue = PriorityTaskQueue::with_weights(weights);Sourcepub async fn enqueue(
&self,
task: Box<dyn Task>,
priority: Priority,
) -> TaskResult<()>
pub async fn enqueue( &self, task: Box<dyn Task>, priority: Priority, ) -> TaskResult<()>
Enqueue a task with the specified priority
§Example
use reinhardt_tasks::{Priority, PriorityTaskQueue};
let queue = PriorityTaskQueue::new();
let task = MyTask::new();
// queue.enqueue(Box::new(task), Priority::High).await?;Sourcepub async fn dequeue(&self) -> TaskResult<Option<Box<dyn Task>>>
pub async fn dequeue(&self) -> TaskResult<Option<Box<dyn Task>>>
Dequeue a task using weighted scheduling
Tasks are selected based on their priority weights. Higher priority tasks have a higher probability of being selected, but lower priority tasks are not starved.
Returns None if the queue is empty.
§Example
use reinhardt_tasks::PriorityTaskQueue;
let queue = PriorityTaskQueue::new();
if let Some(task) = queue.dequeue().await? {
// Process task
}Sourcepub async fn len(&self) -> usize
pub async fn len(&self) -> usize
Get the total number of tasks in all queues
§Example
use reinhardt_tasks::PriorityTaskQueue;
let queue = PriorityTaskQueue::new();
assert_eq!(queue.len().await, 0);Sourcepub async fn is_empty(&self) -> bool
pub async fn is_empty(&self) -> bool
Check if the queue is empty
§Example
use reinhardt_tasks::PriorityTaskQueue;
let queue = PriorityTaskQueue::new();
assert!(queue.is_empty().await);Sourcepub async fn len_for_priority(&self, priority: Priority) -> usize
pub async fn len_for_priority(&self, priority: Priority) -> usize
Get the number of tasks for a specific priority
§Example
use reinhardt_tasks::{Priority, PriorityTaskQueue};
let queue = PriorityTaskQueue::new();
assert_eq!(queue.len_for_priority(Priority::High).await, 0);