我有一个异步函数,想要在后台运行。这个函数是调用层次结构的一部分,而该结构没有使用异步调用。
我的调用层次结构如下:
我的调用层次结构如下:
struct Handler {}
impl Handler {
pub async fn handle(&self) {
/// does some stuff and updates internal caches
}
}
struct Client_v2 {
handler: Handler,
rt: tokio::runtime::Runtime,
}
impl Client_v2 {
fn do_work(&self) {
let future = self.handler(handle);
self.rt.spawn(future); // want this to run in background and not block!
// `self` has an anonymous lifetime `'_` but it needs to satisfy a `'static` lifetime requirement
}
}
据我所知,可能存在一个问题,即客户端 Client_v2
可能会超过其所有者的生命周期,从而导致此错误。
我该如何使生命周期有效?我需要创建线程并将对象移进和移出线程作用域吗?此函数被频繁调用。