Struct TaskController

Source
pub struct TaskController { /* private fields */ }
Expand description

Handles spawning tasks which can also be cancelled by calling cancel on the task controller. If a std::time::Duration is supplied using the with_timeout constructor, then any tasks spawned by the TaskController will automatically be cancelled after the supplied duration has elapsed.

This provides a different API from Context for the same end result. It’s nicer to use when you don’t need child futures to gracefully shutdown. In cases that you do require graceful shutdown of child futures, you will need to pass a Context down, and incorporate the context into normal program flow for the child function so that they can react to it as needed and perform custom asynchronous cleanup logic.

§Examples

use std::time::Duration;
use tokio::time;
use tokio_context::task::TaskController;

async fn task_that_takes_too_long() {
    time::sleep(time::Duration::from_secs(60)).await;
    println!("done");
}

#[tokio::main]
async fn main() {
    let mut controller = TaskController::new();

    let mut join_handles = vec![];

    for i in 0..10 {
        let handle = controller.spawn(async { task_that_takes_too_long().await });
        join_handles.push(handle);
    }

    // Will cancel all spawned contexts.
    controller.cancel();

    // Now all join handles should gracefully close.
    for join in join_handles {
        join.await.unwrap();
    }
}

Implementations§

Source§

impl TaskController

Source

pub fn cancel(self)

Call cancel() to cancel any tasks spawned by this TaskController. You can also simply drop the TaskController to achieve the same result.

Source

pub fn new() -> TaskController

Constructs a new TaskController, which can be used to spawn tasks. Tasks spawned from the task controller will be cancelled if cancel() gets called.

Source

pub fn with_timeout(timeout: Duration) -> TaskController

Constructs a new TaskController, which can be used to spawn tasks. Tasks spawned from the task controller will be cancelled if cancel() gets called. They will also be cancelled if a supplied timeout elapses.

Source

pub fn spawn<T>(&mut self, future: T) -> JoinHandle<Option<T::Output>>
where T: Future + Send + 'static, T::Output: Send + 'static,

Spawns tasks using an identical API to tokio::task::spawn. Tasks spawned from this TaskController will obey the optional timeout that may have been supplied during construction of the TaskController. They will also be cancelled if cancel() is ever called. Returns a JoinHandle from the internally generated task.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.