-
Notifications
You must be signed in to change notification settings - Fork 158
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
Figure out a way to race multiple futures in smol #292
Comments
One way of doing this would be with a channel. let (sender, receiver) = async_channel::bounded(1);
let ex = Executor::new();
let mut handles = vec![];
// Spawn all of the futures.
for future in futures_to_race {
handles.push(ex.spawn({
let sender = send.clone();
async move {
// Send the result of the future down the channel.
sender.send(future.await).await.ok();
}
});
}
// Run the executor while waiting for the result.
let result = ex.run(receiver.recv()).await.unwrap();
for handle in handles {
handle.cancel().await;
} However, this is pretty unwieldy and I would like a way to implement this pattern in a simpler way. Same could probably go for the |
A fully generic channel looks a like a bit too much, why not e.g. a once_cell, an event and a flag (or just some kind of one-shot channel with clonable sending end)? |
Although that would work, it's unfair to expect this burden to fall onto the user. My idea was to create an |
ok, oh some additional prior art would also be https://github.com/YZITE/server-executor, which I wrote to have an executor for tests, simple async servers, etc. |
I think the |
Generally in
smol
we frown on large combinators and prefer using the executor when possible. For instance, instead ofjoin
ing a bunch of different futures, we spawn the futures on anExecutor
and then wait on each of theTask
handles.However this isn't a good solution to
or
orrace
. Ideally we would have a way to spawn a bunch of futures on anExecutor
and then have a way to wait on one of those tasks.cc @i509VCB, @smol-rs/admins Would appreciate brainstorming on a good solution to this problem, as one isn't immediately apparent to me.
The text was updated successfully, but these errors were encountered: