Tokio running loop task

According to Alice's blog, when you running forever you should use a dedicated thread, but I also need a lot of tokio features like channels in my program, the program looks like:

#[tokio::main]
async fn main() {
    loop {
        // total sync code
        let result = processing();
        // some other place handing
        sender.send(result)
    }
}

should I remove tokio and make my code all synchronous?

If you can, why not? This sounds like you have a dependency that you don't need for a feature you don't use (async fn).

Hi, because I need to concurrent handling processed data, could be 3~4 threads, and that's why I want to use tokio.

#[tokio::main]
async fn main() {
    loop {
        let (data1, data2) = (capture_data(), capture_data());
        // total sync code
        let result = processing(data1, data2);
        // some other place handing
        let handle1 = handle_data_spawn(data1);
        let handle2 = handle_data_spawn(data2);
        let handle3 = handle_data_spawn(result);
        wait_for(handle1, handle2, handle3)
    }
}

If you have no .await point you very likely should not use async.

3 Likes

Do you have a restriction that doesn't allow you to create threads? 3-4 threads is a very small number in general.