pub struct TaskPool { /* private fields */ }Expand description
A concurrent task execution pool with semaphore-based concurrency control.
TaskPool manages the execution of asynchronous tasks with a configurable limit on the number of concurrent tasks. It provides cancellation support and automatic resource cleanup.
§Thread Safety
TaskPool is thread-safe and can be cloned to share across multiple contexts. All clones share the same underlying semaphore and cancellation token.
Implementations§
Source§impl TaskPool
impl TaskPool
Sourcepub fn execute<F, T>(&self, func: F)
pub fn execute<F, T>(&self, func: F)
Executes a future in the task pool with concurrency control.
The task will wait for a semaphore permit before executing. If the pool is cancelled while the task is running, it will be interrupted gracefully.
§Type Parameters
F- Future type that implements Send and has a static lifetimeT- Output type of the future that implements Send
§Arguments
func- The async function/future to execute
§Examples
use quetty_server::taskpool::TaskPool;
async fn example() {
let pool = TaskPool::new(3);
pool.execute(async {
println!("Task is running");
// Do some work
});
}Sourcepub fn cancel_all(&self)
pub fn cancel_all(&self)
Cancels all currently running and queued tasks.
This sends a cancellation signal to all tasks. Tasks that are currently executing will be interrupted at their next cancellation check point. Tasks waiting for permits will be cancelled before they start.
§Examples
use quetty_server::taskpool::TaskPool;
async fn example() {
let pool = TaskPool::new(3);
// Start some tasks
for i in 0..10 {
pool.execute(async move {
println!("Task {}", i);
});
}
// Cancel all tasks
pool.cancel_all();
}Sourcepub fn close(&self)
pub fn close(&self)
Closes the task pool to prevent new tasks from starting.
This closes the underlying semaphore, which prevents new tasks from acquiring permits. Tasks that are already running will continue to completion.
§Examples
use quetty_server::taskpool::TaskPool;
async fn example() {
let pool = TaskPool::new(3);
// Use the pool...
// Close it to prevent new tasks
pool.close();
}