代码:
struct TExecuteTask {
count:u32
}
impl Future for TExecuteTask {
type Output = ();
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
println!("future require");
if self.count == 2 {
return Ready(());
}
unsafe {
self.get_unchecked_mut().count += 1;
}
Pending
}
}
pub fn test1() {
let v = TExecuteTask{count:0};
let rt = tokio::runtime::Builder::new_multi_thread().enable_all().build().unwrap();
println!("test trace1");
rt.block_on(v);
println!("test trace2");
}
运行结果:
貌似调用block_on之后会回调一次future的poll,如果这个时候还没有计算出结果,那就直接卡死了,所以需要找个地方能唤醒。