在异步上下文中的同步 Fn 中运行异步 Fn

问题描述 投票:0回答:0

我有一个设置可以归结为:

fn work() {
    // some sync stuff
    // work2()
}

async fn work2() {
    // some async stuff
}

#[tokio::main]
async fn main() {
    work();
}

如何从

work2
中执行
work
?我试过使用
tokio::block_on
它给出了以下错误:

thread 'main' panicked at 'Cannot start a runtime from within a runtime. This 
happens because a function (like `block_on`) attempted to block the current 
thread while the thread is being used to drive asynchronous tasks.'

这是否意味着我必须使用另一个线程?还有其他方法吗?

asynchronous rust rust-tokio
© www.soinside.com 2019 - 2024. All rights reserved.