smol icon indicating copy to clipboard operation
smol copied to clipboard

Equivalent of `tokio::task::JoinSet`

Open joshtriplett opened this issue 1 year ago • 4 comments

I'd love to have an equivalent of tokio::task::JoinSet: a type that provides a spawn method, saves the join handle, and provides an operation to wait for the next task in the set to complete.

In addition to the methods provided by JoinSet, it'd also be nice to have an asynchronous spawn method that takes a concurrency limit, and if there are more than that number of items in the set, waits for the next task to complete before spawning another item.

joshtriplett avatar Jun 02 '24 07:06 joshtriplett

Is there something JoinSet can do that's non-trivial to do with a Vec<Task<()>>? Personally I've never felt the need for it, but if there's a good reason, then we can model it out and consider adding it.

In addition to the methods provided by JoinSet, it'd also be nice to have an asynchronous spawn method that takes a concurrency limit,

This can be done now with a Semaphore, see this example.

notgull avatar Jun 08 '24 15:06 notgull

@notgull A JoinSet isn't ordered; when you wait on the next completion from it, it returns the next completion regardless of its position.

joshtriplett avatar Jun 08 '24 21:06 joshtriplett

@yoshuawuyts This is basically futures-concurrency::FuturesGroup, right?

notgull avatar Jun 08 '24 22:06 notgull

Yes that's right! - Rather than calling spawn though, you call push. And it returns an async iterator of values (Stream trait right now for compat reasons).

Conceptually it's very similar to FuturesUnordered, with the one main difference being that it supports lending iteration - so you can add more futures to the group while iterating over it, without needing to resort to something like select! [^1].

[^1]: The main only caveat I'd add is that I messed up the internal slab implementation, and I really should merge https://github.com/yoshuawuyts/futures-concurrency/pull/187 in the meantime to fix some of that. But beyond that: yes, I think this sounds like it should largely be a match.

yoshuawuyts avatar Jun 09 '24 00:06 yoshuawuyts