Giter Site home page Giter Site logo

Comments (4)

mox692 avatar mox692 commented on June 8, 2024

After the completion of the first loop, subsequent loops are almost always expected to terminate the second arm first, so the tick print will not continue as you expected.

from tokio.

Darksonn avatar Darksonn commented on June 8, 2024

You need to use an if precondition instead of placing the if inside the async block. That way, the branch will be disabled instead of triggering immediately.

See the documentation on select! for more info on if preconditions.

from tokio.

rcitsz avatar rcitsz commented on June 8, 2024

@Darksonn
Thanks!
But in another form.
If the select arm is not async block but async function:

async fn waitfor(s: &mut bool) {
    if *s {
        *s = false;

       //   If comment out this line, the timer will work properly
        tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
    }
}

#[test]
fn test() {
    tokio::runtime::Runtime::new().unwrap().block_on(async {
        tokio::spawn(async move {
            let mut timer = tokio::time::interval(tokio::time::Duration::from_secs(1));
            let mut s = true; 
            loop {
                tokio::select! {
                    _= timer.tick() => {
                       println!("----tick");
                    }
                _ = waitfor(&mut s) => {}
                }
            }
        })
        .await
        .unwrap();
    });
}

from tokio.

Darksonn avatar Darksonn commented on June 8, 2024

You can also await std::future::pending() to have waitfor sleep forever / until timer.tick() triggers.

from tokio.

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.