Hello! I'm new with tokio and was trying to split a vec into chunks to consume it in async tasks. I have this function
async fn query_chunked(tokens: Vec<Address>, address: H160){
let providers = vec![
Arc::new(Provider::<Http>::try_from("https://rpc.mevblocker.io/fast").unwrap()),
Arc::new(Provider::<Http>::try_from("https://rpc.mevblocker.io/fullprivacy").unwrap()),
Arc::new(Provider::<Http>::try_from("https://eth.rpc.blxrbdn.com").unwrap()),
];
let futures: Vec<_> = tokens.chunks(60).map(|token_chunk| tokio::spawn(
query_balance_multiple(Arc::clone(&providers[0]), address, &token_chunk)
))
.collect();
for job in futures.into_iter() {
job.await;
}
}
but it fails at compilation with the following error
error[E0597]: `tokens` does not live long enough
--> src/main.rs:27:27
|
27 | let futures: Vec<_> = tokens.chunks(60).map(|token_chunk| tokio::spawn(
| ^^^^^^^^^^^^^^^^^ borrowed value does not live long enough
28 | query_balance_multiple(Arc::clone(&providers[0]), address, &token_chunk)
| ------------------------------------------------------------------------ argument requires that `tokens` is borrowed for `'static`
...
36 | }
| - `tokens` dropped here while still borrowed
I've tried to search for ways to do it but online I can't find any example. What would be the right way to do spawn the tasks to avoid the dropped variable? Thanks
The easiest solution is to just clone the chunks. If that's really expensive you can use an Arc. In your sample code you need to check the result from awaiting a JoinHandle.