Skip to content

try_join! on parallel tasks #2730

Answered by Darksonn
zaszi asked this question in General
Discussion options

You must be logged in to vote

Okay so first, the error

#[tokio::main]
^^^^^^^^^^^^^^ expected struct `std::boxed::Box`, found struct `tokio::task::JoinError`

is due to this line:

if let Err(e) = res { return Err(e) };

The type of e here is not a Box<dyn Error>, so the return fails. You can do return Err(e.into()); to convert the error to a box.

As for cancellation of tasks, first not that tokio::spawn wraps the thing in a Result in case the spawned task panics, so if the tasks also return Results, you will want some sort of flatten that turns Result<Result<T, E1>, E2> into a Result<T, E> to properly notice the inner errors.

As for cancelling tasks, you can use a broadcast channel combined with select! to cancel many…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by zaszi
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants