有没有办法在 tokio 0.2 中将 tokio::main 与单线程运行时一起使用?

use*_*732 2 rust-tokio

有没有办法使用#[tokio::main]tokio 0.2 中的属性指定单线程运行时?该文档似乎没有这方面的示例。

编辑:我想找到一种方法来设置 tokio 运行时,以便rustc知道tokio:spawn()不会是一个新线程。

She*_*ter 6

的文档tokio::main显示了它有哪些选项:

core_threads=n- 将核心线程设置为 n(需要 rt-threaded 功能)。 max_threads=n- 将最大线程数设置为 n(需要 rt-core 或 rt-threaded 功能)。

因此:

#[tokio::main(core_threads = 1, max_threads = 1)]
async fn main() {
    println!("Hello world");
}
Run Code Online (Sandbox Code Playgroud)

如果这不适用于您的任何情况,您将必须直接创建一个运行时,如如何同步返回在稳定 Rust 中的异步 Future 中计算的值中所示?.