pub struct TaskSupervisor { /* private fields */ }Expand description
Manages a collection of asynchronous tasks and coordinates their shutdown.
TaskSupervisor wraps TaskTracker to keep count of outstanding tasks while also exposing a
process-wide CancellationToken that can be used to request cooperative shutdown.
§Examples
use tokio_task_supervisor::TaskSupervisor;
use tokio::time::{sleep, Duration};
#[tokio::main]
async fn main() {
let supervisor = TaskSupervisor::new();
// Spawn a task that cooperatively handles cancellation
let handle = supervisor.spawn_with_token(|token| async move {
loop {
if token.is_cancelled() {
break;
}
// Do work...
sleep(Duration::from_millis(100)).await;
}
});
// Later, request shutdown
supervisor.shutdown().await;
}Implementations§
Source§impl TaskSupervisor
impl TaskSupervisor
Sourcepub fn tracker(&self) -> &TaskTracker
pub fn tracker(&self) -> &TaskTracker
Returns a reference to the underlying TaskTracker.
Sourcepub fn token(&self) -> CancellationToken
pub fn token(&self) -> CancellationToken
Returns a clone of the shared cancellation token.
Sourcepub fn cancel_on_drop(&self) -> DropGuardRef<'_>
pub fn cancel_on_drop(&self) -> DropGuardRef<'_>
Returns a guard that cancels the shutdown token when dropped.
Sourcepub fn is_cancelled(&self) -> bool
pub fn is_cancelled(&self) -> bool
Returns true if the shutdown token has been cancelled.
Sourcepub fn wait(&self) -> TaskTrackerWaitFuture<'_> ⓘ
pub fn wait(&self) -> TaskTrackerWaitFuture<'_> ⓘ
Returns a future that completes when all tasks finish.
Sourcepub fn cancel(&self)
pub fn cancel(&self)
Cancels the shared shutdown token.
Tasks spawned through the managed API can observe this and exit cooperatively.
Sourcepub async fn shutdown(&self)
pub async fn shutdown(&self)
Initiates graceful shutdown by closing the tracker and cancelling all tasks.
This method will:
- Close the task tracker to prevent new tasks from being spawned
- Cancel the shutdown token to signal all existing tasks
- Wait for all tasks to complete
Sourcepub fn spawn_with_cancel<F, Fut>(
&self,
task: F,
) -> JoinHandle<CancelOutcome<Fut::Output>>
pub fn spawn_with_cancel<F, Fut>( &self, task: F, ) -> JoinHandle<CancelOutcome<Fut::Output>>
Spawns a task that races against the shared cancellation token.
The returned future resolves with CancelOutcome, indicating whether the task finished
normally or was cancelled. When cancellation wins the race, the task future is dropped, so it
should not rely on Drop for cleanup.
§Arguments
task- A closure that returns the future to execute
§Returns
A JoinHandle that resolves to the task’s outcome
Sourcepub fn spawn_on_with_cancel<F, Fut>(
&self,
task: F,
handle: &Handle,
) -> JoinHandle<CancelOutcome<Fut::Output>>
pub fn spawn_on_with_cancel<F, Fut>( &self, task: F, handle: &Handle, ) -> JoinHandle<CancelOutcome<Fut::Output>>
Sourcepub fn spawn_local_with_cancel<F, Fut>(
&self,
task: F,
) -> JoinHandle<CancelOutcome<Fut::Output>>
pub fn spawn_local_with_cancel<F, Fut>( &self, task: F, ) -> JoinHandle<CancelOutcome<Fut::Output>>
Sourcepub fn spawn_local_on_with_cancel<F, Fut>(
&self,
task: F,
local_set: &LocalSet,
) -> JoinHandle<CancelOutcome<Fut::Output>>
pub fn spawn_local_on_with_cancel<F, Fut>( &self, task: F, local_set: &LocalSet, ) -> JoinHandle<CancelOutcome<Fut::Output>>
Sourcepub fn spawn_with_token<F, Fut>(&self, task: F) -> JoinHandle<Fut::Output>
pub fn spawn_with_token<F, Fut>(&self, task: F) -> JoinHandle<Fut::Output>
Sourcepub fn spawn_on_with_token<F, Fut>(
&self,
task: F,
handle: &Handle,
) -> JoinHandle<Fut::Output>
pub fn spawn_on_with_token<F, Fut>( &self, task: F, handle: &Handle, ) -> JoinHandle<Fut::Output>
Sourcepub fn spawn_local_with_token<F, Fut>(&self, task: F) -> JoinHandle<Fut::Output>
pub fn spawn_local_with_token<F, Fut>(&self, task: F) -> JoinHandle<Fut::Output>
Sourcepub fn spawn_local_on_with_token<F, Fut>(
&self,
task: F,
local_set: &LocalSet,
) -> JoinHandle<Fut::Output>
pub fn spawn_local_on_with_token<F, Fut>( &self, task: F, local_set: &LocalSet, ) -> JoinHandle<Fut::Output>
Sourcepub fn spawn_blocking_with_token<F, T>(&self, task: F) -> JoinHandle<T>
pub fn spawn_blocking_with_token<F, T>(&self, task: F) -> JoinHandle<T>
Sourcepub fn spawn_blocking_on_with_token<F, T>(
&self,
task: F,
handle: &Handle,
) -> JoinHandle<T>
pub fn spawn_blocking_on_with_token<F, T>( &self, task: F, handle: &Handle, ) -> JoinHandle<T>
Trait Implementations§
Source§impl Clone for TaskSupervisor
impl Clone for TaskSupervisor
Source§fn clone(&self) -> TaskSupervisor
fn clone(&self) -> TaskSupervisor
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more