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;
}§Deref Implementation
TaskSupervisor implements Deref targeting TaskTracker, allowing you to call
TaskTracker methods directly on a TaskSupervisor instance:
use tokio_task_supervisor::TaskSupervisor;
#[tokio::main]
async fn main() {
let supervisor = TaskSupervisor::new();
// These calls work through deref coercion:
let handle = supervisor.spawn(async { 42 });
let count = supervisor.len();
let is_closed = supervisor.is_closed();
// Equivalent to:
let handle = supervisor.tracker().spawn(async { 42 });
let count = supervisor.tracker().len();
let is_closed = supervisor.tracker().is_closed();
}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>
Methods from Deref<Target = TaskTracker>§
Sourcepub fn wait(&self) -> TaskTrackerWaitFuture<'_> ⓘ
pub fn wait(&self) -> TaskTrackerWaitFuture<'_> ⓘ
Waits until this TaskTracker is both closed and empty.
If the TaskTracker is already closed and empty when this method is called, then it
returns immediately.
The wait future is resistant against ABA problems. That is, if the TaskTracker
becomes both closed and empty for a short amount of time, then it is guarantee that all
wait futures that were created before the short time interval will trigger, even if they
are not polled during that short time interval.
§Cancel safety
This method is cancel safe.
However, the resistance against ABA problems is lost when using wait as the
condition in a tokio::select! loop.
Sourcepub fn close(&self) -> bool
pub fn close(&self) -> bool
Close this TaskTracker.
This allows wait futures to complete. It does not prevent you from spawning new tasks.
Returns true if this closed the TaskTracker, or false if it was already closed.
Sourcepub fn reopen(&self) -> bool
pub fn reopen(&self) -> bool
Reopen this TaskTracker.
This prevents wait futures from completing even if the TaskTracker is empty.
Returns true if this reopened the TaskTracker, or false if it was already open.
Sourcepub fn spawn<F>(&self, task: F) -> JoinHandle<<F as Future>::Output>
pub fn spawn<F>(&self, task: F) -> JoinHandle<<F as Future>::Output>
Spawn the provided future on the current Tokio runtime, and track it in this TaskTracker.
This is equivalent to tokio::spawn(tracker.track_future(task)).
Sourcepub fn spawn_on<F>(
&self,
task: F,
handle: &Handle,
) -> JoinHandle<<F as Future>::Output>
pub fn spawn_on<F>( &self, task: F, handle: &Handle, ) -> JoinHandle<<F as Future>::Output>
Spawn the provided future on the provided Tokio runtime, and track it in this TaskTracker.
This is equivalent to handle.spawn(tracker.track_future(task)).
Sourcepub fn spawn_local<F>(&self, task: F) -> JoinHandle<<F as Future>::Output>
pub fn spawn_local<F>(&self, task: F) -> JoinHandle<<F as Future>::Output>
Spawn the provided future on the current LocalSet, and track it in this TaskTracker.
This is equivalent to tokio::task::spawn_local(tracker.track_future(task)).
Sourcepub fn spawn_local_on<F>(
&self,
task: F,
local_set: &LocalSet,
) -> JoinHandle<<F as Future>::Output>
pub fn spawn_local_on<F>( &self, task: F, local_set: &LocalSet, ) -> JoinHandle<<F as Future>::Output>
Spawn the provided future on the provided LocalSet, and track it in this TaskTracker.
This is equivalent to local_set.spawn_local(tracker.track_future(task)).
Sourcepub fn spawn_blocking<F, T>(&self, task: F) -> JoinHandle<T>
pub fn spawn_blocking<F, T>(&self, task: F) -> JoinHandle<T>
Spawn the provided blocking task on the current Tokio runtime, and track it in this TaskTracker.
This is equivalent to tokio::task::spawn_blocking(tracker.track_future(task)).
Sourcepub fn spawn_blocking_on<F, T>(&self, task: F, handle: &Handle) -> JoinHandle<T>
pub fn spawn_blocking_on<F, T>(&self, task: F, handle: &Handle) -> JoinHandle<T>
Spawn the provided blocking task on the provided Tokio runtime, and track it in this TaskTracker.
This is equivalent to handle.spawn_blocking(tracker.track_future(task)).
Sourcepub fn track_future<F>(&self, future: F) -> TrackedFuture<F> ⓘwhere
F: Future,
pub fn track_future<F>(&self, future: F) -> TrackedFuture<F> ⓘwhere
F: Future,
Track the provided future.
The returned TrackedFuture will count as a task tracked by this collection, and will
prevent calls to wait from returning until the task is dropped.
The task is removed from the collection when it is dropped, not when poll returns
Poll::Ready.
§Examples
Track a future spawned with tokio::spawn.
use tokio_util::task::TaskTracker;
let tracker = TaskTracker::new();
tokio::spawn(tracker.track_future(my_async_fn()));Track a future spawned on a JoinSet.
use tokio::task::JoinSet;
use tokio_util::task::TaskTracker;
let tracker = TaskTracker::new();
let mut join_set = JoinSet::new();
join_set.spawn(tracker.track_future(my_async_fn()));Sourcepub fn token(&self) -> TaskTrackerToken
pub fn token(&self) -> TaskTrackerToken
Creates a TaskTrackerToken representing a task tracked by this TaskTracker.
This token is a lower-level utility than the spawn methods. Each token is considered to
correspond to a task. As long as the token exists, the TaskTracker cannot complete.
Furthermore, the count returned by the len method will include the tokens in the count.
Dropping the token indicates to the TaskTracker that the task has exited.
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