I was using tokio::spawn initially with which the I can call my test().await
.......
if let Ok(result) = request::Request::get_request().await{
let message = result.message().iter();
for req in message{
log::debug!("Detected request id: {}",req.id());
println!("{:?}",req.config());
tokio::spawn(async move{
println!("Processing request id: {}",req.id());
test().await;
});
}
}
However, am getting a lifetime issue:
19 | let message = result.message().iter();
| ^^^^^^----------
| |
| borrowed value does not live long enough
| argument requires that `result` is borrowed for `'static`
...
58 | }
| - `result` dropped here while still borrowed
Thats why I used crossbeam scope to prevent this issue. Any suggestions would be appreciable thanks
Unfortunately there is no asynchronous equivalent to scoped threads. You have to take ownership of the value in some way to move it into the task. Usually you do this by either calling a method like into_iter() rather than iter() or by cloning, but as I don't know what the types involved are, I can't tell you which of these apply to your situation.
Then you would have to clone the RequestData before spawning. The only way to avoid the clone is to take ownership of the data in the vector, which you can't do through an immutable reference.
I will note that returning &Vec<T> is unidiomatic. The type &[T] is preferred.