Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to split a JoinSet into spawn and join halves #6547

Closed
slonkazoid opened this issue May 9, 2024 · 1 comment
Closed

Ability to split a JoinSet into spawn and join halves #6547

slonkazoid opened this issue May 9, 2024 · 1 comment
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-task Module: tokio/task

Comments

@slonkazoid
Copy link

Is your feature request related to a problem? Please describe.

JoinSet requires a mutable reference to spawn or join tasks. This means you cannot have 2 concurrent tasks appending tasks and joining them.
This may be required for example when you have to spawn multiple millions of tasks so you use a semaphore and acquire a ticket before you spawn a new task.

Describe the solution you'd like

A simple fn split(self) -> (SpawnerHalf, JoinerHalf) method like this:

let (mut spawner, mut joiner) = JoinSet::new().split();

let task_a = tokio::task::spawn(async move {
    spawner.spawn(async { 42i32 });
});

while let Some(joined) = joiner.join_next().await {
  println!("the meaning of life is: {}", joined.unwrap());
}

task_a.await.unwrap();

Describe alternatives you've considered

  • Mutex. Deadlocks with no obvious way to fix it.
  • Making a JoinSet equivalent that takes a &self so it can be put in an Arc and cloned around.
  • Passing my own waker into join_next().
@slonkazoid slonkazoid added A-tokio Area: The main tokio crate C-feature-request Category: A feature request. labels May 9, 2024
@Darksonn Darksonn added the M-task Module: tokio/task label May 11, 2024
@Darksonn
Copy link
Contributor

This is not a use-case that JoinSet supports. Some alternatives:

  1. If you don't need return values, then consider the TaskTracker.
  2. If you need return values, then consider sending them using a mpsc channel.

@Darksonn Darksonn closed this as not planned Won't fix, can't repro, duplicate, stale May 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-feature-request Category: A feature request. M-task Module: tokio/task
Projects
None yet
Development

No branches or pull requests

2 participants