Concurrent rest api calls

Hi,

Since there is not much work between random api calls idea is to call them concurrently, wait for the result, and in the meantime thread go back to do something else and check back again.

What is the best way to implement this in Rust? Following is experimental code but call does not complete in the background.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=043151838d3dd031c4d6039718d397db

I cannot use join all as rest calls are made on random occurring events. Ideally just post the call and check the future later for data.

Thanks in advance

That problem is why their is so much talk about Futures and asyc/await in the Rust world.

Threads are good when you have a lot of compute work to do that can be done on different cores.

Async is good when you have to wait for many things at the same time, like API call responses.

But it is not completing the call in the background, it seems to get stuck.

pool.run_until_stalled();
println!("# Sleeping for 10 seconds, rest calls should finish by then.");
thread::sleep(Duration::from_secs(10));

Rest call should have been finished after sleep and data available. But I've to call block_on or run again to complete it.

I know almost nothing about async/await or tokio in Rust and have never tried to use them but from the documentation I read:

"This function [run_until_stalled] will not block the calling thread and will return the moment that there are no tasks left for which progress can be made; remaining incomplete tasks in the pool can continue with further use of one of the pool's run or poll methods. While the function is running, all tasks in the pool will try to make progress."

That hints to me that you have "remaining incomplete tasks" so you are not getting the result you want and that you should loop on run_until_stalled() until all the work is actually done.

Your 10 second sleep does not help. During that time none of your async stuff can make progress, you are not running it. And you have blocked your thread anyway so nothing is happening. There is no "background".

I could be way off base here mind.

I tried looping, it is not working. But the idea is not to loop more than few times and check later if data is there.

I just don't see how we can expect the async engine to run and handle the events required to get the job done if we are not actually running said async engine.

I don't know, I'll be interested to see what anyone suggests.