pub struct TaskQueue { /* private fields */ }Expand description
A thread-based task queue with configurable concurrency and priority scheduling.
Workers continuously pull the highest-priority task from the queue and execute it.
When the queue is shut down, running tasks are allowed to complete but pending
tasks are dropped (their handles will receive TaskError::Cancelled).
§Example
use philiprehberger_task_queue::{TaskQueue, Priority};
let queue = TaskQueue::new(2);
let h1 = queue.submit(|| 10);
let h2 = queue.submit_with_priority(Priority::High, || 20);
assert_eq!(h1.join().unwrap(), 10);
assert_eq!(h2.join().unwrap(), 20);
queue.shutdown();Implementations§
Source§impl TaskQueue
impl TaskQueue
Sourcepub fn new(concurrency: usize) -> Self
pub fn new(concurrency: usize) -> Self
Create a new task queue with the given number of worker threads.
§Panics
Panics if concurrency is zero.
Sourcepub fn submit<F, T>(&self, task: F) -> TaskHandle<T>
pub fn submit<F, T>(&self, task: F) -> TaskHandle<T>
Submit a task with Normal priority.
Returns a TaskHandle that can be used to retrieve the result.
Sourcepub fn submit_with_priority<F, T>(
&self,
priority: Priority,
task: F,
) -> TaskHandle<T>
pub fn submit_with_priority<F, T>( &self, priority: Priority, task: F, ) -> TaskHandle<T>
Submit a task with the given priority.
Higher-priority tasks are executed before lower-priority ones when multiple tasks are waiting in the queue.
Returns a TaskHandle that can be used to retrieve the result.
If the queue is draining or shut down, the returned handle will
immediately yield TaskError::Cancelled.
Sourcepub fn stats(&self) -> TaskQueueStats
pub fn stats(&self) -> TaskQueueStats
Return a snapshot of task queue statistics.
The counters are updated atomically as tasks are submitted, completed, and failed, so successive calls may return different values.
§Example
use philiprehberger_task_queue::TaskQueue;
let queue = TaskQueue::new(1);
let handle = queue.submit(|| 1 + 1);
handle.join().unwrap();
let stats = queue.stats();
assert_eq!(stats.total_submitted, 1);
assert_eq!(stats.completed, 1);
queue.shutdown();Sourcepub fn drain(self)
pub fn drain(self)
Stop accepting new tasks and wait for all queued and in-flight tasks to complete.
Unlike shutdown, drain does not drop
pending tasks — every task that was already submitted will run to
completion. New submissions made after drain is called will be
immediately cancelled.
This method blocks until the queue is empty and all workers are idle, then shuts down the worker threads.
§Example
use philiprehberger_task_queue::TaskQueue;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
let queue = TaskQueue::new(2);
let counter = Arc::new(AtomicUsize::new(0));
for _ in 0..5 {
let c = counter.clone();
queue.submit(move || { c.fetch_add(1, Ordering::SeqCst); });
}
queue.drain();
assert_eq!(counter.load(Ordering::SeqCst), 5);Sourcepub fn on_complete<F>(&self, callback: F)
pub fn on_complete<F>(&self, callback: F)
Register a callback that fires after each task completes.
The callback receives two arguments:
success—trueif the task completed without panicking,falseotherwise.duration— wall-clock time the task took to execute.
Only one callback may be active at a time; calling this again replaces the previous callback.
§Example
use philiprehberger_task_queue::TaskQueue;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
let queue = TaskQueue::new(1);
let count = Arc::new(AtomicUsize::new(0));
let c = count.clone();
queue.on_complete(move |_success, _dur| {
c.fetch_add(1, Ordering::SeqCst);
});
queue.submit(|| 42).join().unwrap();
assert_eq!(count.load(Ordering::SeqCst), 1);
queue.shutdown();