pub struct Tasker { /* private fields */ }Expand description
Manages a group of tasks.
See library-level documentation.
Implementations§
Source§impl Tasker
impl Tasker
Sourcepub fn add_handle(&self, handle: JoinHandle<()>)
pub fn add_handle(&self, handle: JoinHandle<()>)
Add a tokio task handle to the task group.
It is your responsibility to make sure the task
is stopped by this group’s Stopper future.
Sourcepub fn spawn<F>(&self, future: F)
pub fn spawn<F>(&self, future: F)
Spawn a !Send future the local task set and add its handle to the task group.
It is your responsibility to make sure the task
is stopped by this group’s Stopper future.
Sourcepub fn spawn_local<F>(&self, future: F)
pub fn spawn_local<F>(&self, future: F)
Spawn a tokio task and add its handle to the task group.
See tokio::task::spawn_local().
It is your responsibility to make sure the task
is stopped by this group’s Stopper future.
Sourcepub fn stopper(&self) -> Stopper ⓘ
pub fn stopper(&self) -> Stopper ⓘ
Dispense a Stopper, a future that will resolve once .stop()
is called on any Tasker (or signaller Signaller) clone.
The Stopper future can be used with our .unless()
extension to stop Futures, with .take_until() to stop Streams,
as part of tokio::select!(), and similar…
Sourcepub fn stop(&self) -> bool
pub fn stop(&self) -> bool
Stop the tasks in the group.
This will resolve all Stopper futures (including ones obtained after this call).
Returns true if this was the first effective stop call
or false if the group was already signalled to stop.
Sourcepub fn is_stopped(&self) -> bool
pub fn is_stopped(&self) -> bool
true if stopping was already signalled.
Sourcepub async fn join(self)
pub async fn join(self)
Join all the tasks in the group.
This function will panic if any of the tasks panicked.
Use try_join() if you need to handle task panics yourself.
Note that join() will only return once all other Tasker clones are finished
(via finish() or drop).
Sourcepub async fn try_join(self) -> Vec<Result<(), JoinError>>
pub async fn try_join(self) -> Vec<Result<(), JoinError>>
Join all the tasks in the group, returning a vector of join results
(ie. results of tokio’s JoinHandle).
Note that try_join() will only return once all other Tasker clones are finished
(via finish() or drop).
Sourcepub fn join_stream(self) -> JoinStream
pub fn join_stream(self) -> JoinStream
Returns a Stream which yields join results as they become available,
ie. as the tasks terminate.
If any of the tasks terminates earlier than others, such as due to a panic, its result should be readily avialable through this stream.
The join stream stops after all tasks are joined and all other Tasker
clones are finished (via finish() or drop).
Sourcepub fn finish(self)
pub fn finish(self)
Mark this Tasker clone as finished.
This has the same effect as dropping the clone, but is more explicit. This lets the task group know that no new tasks will be added through this clone.
All Tasker clones need to be finished/dropped in order for .join()
to be able to join all the tasks.
Use .signaller() to get a special Tasker clone that doesn’t
need to be dropped and can be used to .stop() the task group.
Sourcepub fn poll_join(&self) -> usize
pub fn poll_join(&self) -> usize
Poll tasks once and join those that are finished.
Handles to tasks that are finished executing will be removed from the internal storage.
Returns the number of tasks that were joined.
This function will panic if any of the tasks panicked.
Use try_poll_join() if you need to handle task panics yourself.
Sourcepub fn try_poll_join(&self) -> Vec<Result<(), JoinError>>
pub fn try_poll_join(&self) -> Vec<Result<(), JoinError>>
Poll tasks once and join those that are already done.
Handles to tasks that are already finished executing will be joined and removed from the internal storage.
Returns vector of join results of tasks that were joined (may be empty if no tasks could’ve been joined).