GithubHelp home page GithubHelp logo

Comments (5)

Ralith avatar Ralith commented on August 16, 2024

almost exactly the semantics I want (the exact semantics would require select! shenanigans).

Did you see try_join?

from tokio-uring.

matklad avatar matklad commented on August 16, 2024

Yeah, try_join is close, but it doesn't support graceful cancellation. Here's what I came up with for my use-case (hiding behind details to not distract from the main idea of "structured concurrency works with thread-per-core"):

pub async fn try_par<E, F1, F2>(token: &CancellationToken, f1: F1, f2: F2) -> Result<(), E>
where
    F1: Future<Output = Result<(), E>>,
    F2: Future<Output = Result<(), E>>,
{
    let f1 = f1.fuse();
    let f2 = f2.fuse();
    tokio::pin!(f1, f2);

    let mut rs: [Option<Result<(), E>>; 2] = [None, None];
    loop {
        rs = match rs {
            [Some(r1), Some(r2)] => return r1.or(r2),
            _ => rs,
        };
        let (r, _) = futures_util::future::select(&mut f1, &mut f2).await.factor_first();
        if r.is_err() {
            token.cancel();
        }
        *if rs[0].is_none() { &mut rs[0] } else { &mut rs[1] } = Some(r);
    }
}

from tokio-uring.

Noah-Kennedy avatar Noah-Kennedy commented on August 16, 2024

@matklad I'm not sure really that this falls within the scope of tokio-uring. IMO the goal of tokio-uring is to figure out the kinks and quirks of io_uring and work things out from an API perspective before eventually implementing the finalized API into tokio. In particular it would be great to get the API to work on the multi threaded runtime.

I think a better place for this would be in tokio-util or tokio itself. You could probably build something around localsets that would work for this.

from tokio-uring.

Noah-Kennedy avatar Noah-Kennedy commented on August 16, 2024

Closing this as its probably a better question for the broader tokio project.

from tokio-uring.

matklad avatar matklad commented on August 16, 2024

For posterity, the API along these lines was implemented in the moro crate: https://github.com/nikomatsakis/moro

from tokio-uring.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.