pub struct JoinSet<T> { /* private fields */ }
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.unwrap() {
seen[res] = true;
}
for i in 0..10 {
assert!(seen[i]);
}
}
Implementations
sourceimpl<T> JoinSet<T>
impl<T> JoinSet<T>
sourceimpl<T: 'static> JoinSet<T>
impl<T: 'static> JoinSet<T>
sourcepub fn spawn<F>(&mut self, task: F) where
F: Future<Output = T>,
F: Send + 'static,
T: Send,
This is supported on tokio_unstable
only.
pub fn spawn<F>(&mut self, task: F) where
F: Future<Output = T>,
F: Send + 'static,
T: Send,
tokio_unstable
only.Spawn the provided task on the JoinSet
.
Panics
This method panics if called outside of a Tokio runtime.
sourcepub fn spawn_on<F>(&mut self, task: F, handle: &Handle) where
F: Future<Output = T>,
F: Send + 'static,
T: Send,
This is supported on tokio_unstable
only.
pub fn spawn_on<F>(&mut self, task: F, handle: &Handle) where
F: Future<Output = T>,
F: Send + 'static,
T: Send,
tokio_unstable
only.Spawn the provided task on the provided runtime and store it in this JoinSet
.
sourcepub fn spawn_local<F>(&mut self, task: F) where
F: Future<Output = T>,
F: 'static,
This is supported on tokio_unstable
only.
pub fn spawn_local<F>(&mut self, task: F) where
F: Future<Output = T>,
F: 'static,
tokio_unstable
only.sourcepub fn spawn_local_on<F>(&mut self, task: F, local_set: &LocalSet) where
F: Future<Output = T>,
F: 'static,
This is supported on tokio_unstable
only.
pub fn spawn_local_on<F>(&mut self, task: F, local_set: &LocalSet) where
F: Future<Output = T>,
F: 'static,
tokio_unstable
only.Spawn the provided task on the provided LocalSet
and store it in this JoinSet
.
sourcepub async fn join_one(&mut self) -> Result<Option<T>, JoinError>
This is supported on tokio_unstable
only.
pub async fn join_one(&mut self) -> Result<Option<T>, JoinError>
tokio_unstable
only.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
.
sourcepub fn abort_all(&mut self)
This is supported on tokio_unstable
only.
pub fn abort_all(&mut self)
tokio_unstable
only.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.
sourcepub fn detach_all(&mut self)
This is supported on tokio_unstable
only.
pub fn detach_all(&mut self)
tokio_unstable
only.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
Auto Trait Implementations
impl<T> !RefUnwindSafe for JoinSet<T>
impl<T> Send for JoinSet<T> where
T: Send,
impl<T> Sync for JoinSet<T> where
T: Send,
impl<T> Unpin for JoinSet<T>
impl<T> !UnwindSafe for JoinSet<T>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
fn instrument(self, span: Span) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
sourcefn in_current_span(self) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
fn in_current_span(self) -> Instrumented<Self>ⓘNotable traits for Instrumented<T>impl<T> Future for Instrumented<T> where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T> where
T: Future, type Output = <T as Future>::Output;
where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T> where
T: Future, type Output = <T as Future>::Output;
where
S: Into<Dispatch>,
T: Future, type Output = <T as Future>::Output;
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T> where
T: Future, type Output = <T as Future>::Output;
fn with_current_subscriber(self) -> WithDispatch<Self>ⓘNotable traits for WithDispatch<T>impl<T> Future for WithDispatch<T> where
T: Future, type Output = <T as Future>::Output;
T: Future, type Output = <T as Future>::Output;
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more