pub struct JoinSet<T> { /* private fields */ }
Available on crate feature rt and tokio_unstable only.
Expand description

A collection of tasks spawned on a Tokio runtime.

A JoinSet can be used to await the completion of some or all of the tasks in the set. The set is not ordered, and the tasks will be returned in the order they complete.

All of the tasks must have the same return type T.

When the JoinSet is dropped, all tasks in the JoinSet are immediately aborted.

Note: This is an unstable API. The public API of this type may break in 1.x releases. See the documentation on unstable features for details.

Examples

Spawn multiple tasks and wait for them.

use tokio::task::JoinSet;

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

    for i in 0..10 {
        set.spawn(async move { i });
    }

    let mut seen = [false; 10];
    while let Some(res) = set.join_one().await {
        let idx = res.unwrap();
        seen[idx] = true;
    }

    for i in 0..10 {
        assert!(seen[i]);
    }
}

Implementations

Create a new JoinSet.

Returns the number of tasks currently in the JoinSet.

Returns whether the JoinSet is empty.

Available on crate feature tracing only.

Returns a Builder that can be used to configure a task prior to spawning it on this JoinSet.

Examples
use tokio::task::JoinSet;

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

    // Use the builder to configure a task's name before spawning it.
    set.build_task()
        .name("my_task")
        .spawn(async { /* ... */ });
}

Spawn the provided task on the JoinSet, returning an AbortHandle that can be used to remotely cancel the task.

Panics

This method panics if called outside of a Tokio runtime.

Spawn the provided task on the provided runtime and store it in this JoinSet returning an AbortHandle that can be used to remotely cancel the task.

Spawn the provided task on the current LocalSet and store it in this JoinSet, returning an AbortHandle that can be used to remotely cancel the task.

Panics

This method panics if it is called outside of a LocalSet.

Spawn the provided task on the provided LocalSet and store it in this JoinSet, returning an AbortHandle that can be used to remotely cancel the task.

Waits until one of the tasks in the set completes and returns its output.

Returns None if the set is empty.

Cancel Safety

This method is cancel safe. If join_one is used as the event in a tokio::select! statement and some other branch completes first, it is guaranteed that no tasks were removed from this JoinSet.

Waits until one of the tasks in the set completes and returns its output, along with the task ID of the completed task.

Returns None if the set is empty.

When this method returns an error, then the id of the task that failed can be accessed using the JoinError::id method.

Cancel Safety

This method is cancel safe. If join_one_with_id is used as the event in a tokio::select! statement and some other branch completes first, it is guaranteed that no tasks were removed from this JoinSet.

Aborts all tasks and waits for them to finish shutting down.

Calling this method is equivalent to calling abort_all and then calling join_one in a loop until it returns None.

This method ignores any panics in the tasks shutting down. When this call returns, the JoinSet will be empty.

Aborts all tasks on this JoinSet.

This does not remove the tasks from the JoinSet. To wait for the tasks to complete cancellation, you should call join_one in a loop until the JoinSet is empty.

Removes all tasks from this JoinSet without aborting them.

The tasks removed by this call will continue to run in the background even if the JoinSet is dropped.

Trait Implementations

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Executes the destructor for this type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more