Calling Reqwest in loop is slow

Im stuck to run API calls in infinite loop.

This code call the API request sequentially. If each API request takes 1 sec, it takes 10 sec for 10 calls. (I thought this code is concurrent due to await but the result doesnt seem like concurrent. Do I misunderstand Rust await?)

#[tokio::main]
async fn main() {
    let client = reqwest::Client::builder().build().unwrap();
    loop {
        let response = client.get("https://example.com").send().await;
        println!("{:?}", thread::current().id());
    }
}

However, this JS code call the API concurrently, so as soon as the API returns response, it shows the success message. Not necessarily takes 10 sec for 10 calls.

(async () => {
    for (;;) {
        await fetch("https://example.com").then(() => {
            console.log('success')
        }).catch(() => console.error('error'));
    }
})()

I wanna call API in Rust like JS (concurrent instead of sequential). I saw some solutions with futures::stream or tokio join, but I dont think I cant use it here due to infinite loop.
How to make it work concurrently like JS code?

Read the tokio docs, specifically the bit on concurrency.

1 Like

Thanks. I need to re-read Tokio book again.

Solution (in case people are interested):

tokio::spawn(async move {
  // called reqwest here
});
1 Like

Your description of how the JS code works is incorrect. It also processes the requests one at a time. look at this slightly adjusted code snippet:

(async () => {
    for (i = 0;i<10;i++) {
    	console.log("starting request")
        await fetch("https://example.com").then(() => {
            console.log('success')
        }).catch(() => console.error('error'));
    }
})()

when run you will observe the console output alternating between starting request and success.

async isn't just "run things magically in parallel". the await keyword in both languages means "pause execution of this function until the future I'm calling is ready". Their behavior is much more alike than you seem to believe.

P.S. JS and rust are very different languages, I just mean to say that for the examples you posted, they're doing pretty much the same thing.

P.P.S. if you remove the await keyword, then javascript will send multiple requests at once.

2 Likes