I am getting data from the open weather map API. Currently the data is being retrieved synchronously which is slow. However, the function has to be synchronous as it is part of a library, but it can call an async function. How might I still make concurrent requests to increase performance? A solution that does not use reqwests works, but reqwests is preferred.
fn get_combined_data(open_weather_map_api_url: String, open_weather_map_api_key: String,
coordinates: Vec<String>, metric: bool) -> Vec<HashMap<String, String>> {
let urls: Vec<String> = get_urls(open_weather_map_api_url, open_weather_map_api_key,
coordinates.get(0).expect("Improper coordinates").to_string() + "," +
coordinates.get(1).expect("Improper coordinates"), metric);
let mut data: Vec<HashMap<String, String>> = Vec::new();
for url in urls {
let request = reqwest::blocking::get(url).expect("Url Get failed").json().expect("json expected");
data.push(request);
}
return data;
}
3条答案
按热度按时间dba5bblo1#
The easiest is probably to use tokios
new_current_thread
runtime and blocking on the data retreival.nwnhqdif2#
You need an asynchronous runtime in order to call asynchronous functions. The easiest way to get one is to use the
#[tokio::main]
attribute (which despite the name can be applied to any function):h43kikqp3#
If your program isn't already async, probably the easiest way might be to use rayon .