Asynchronous 使用Tokio轮询未来的最小功能集是什么?
我想轮询一个异步函数:Asynchronous 使用Tokio轮询未来的最小功能集是什么?,asynchronous,rust,future,tokio,Asynchronous,Rust,Future,Tokio,我想轮询一个异步函数: #[tokio::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { some_function().await; } 哪些是必需的 full = [ "fs", "io-util", "io-std", "macros", "net"
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
some_function().await;
}
哪些是必需的
full = [
"fs",
"io-util",
"io-std",
"macros",
"net",
"parking_lot",
"process",
"rt",
"rt-multi-thread",
"signal",
"sync",
"time",
]
要使用Tokio轮询未来,您需要一个 这仅在板条箱功能
rt
上受支持
fn main()->结果{
tokio::runtime::Builder::新建当前线程()
.build()
.unwrap()
.block_on(某些函数())
}
async fn some_function()->结果{
好(())
}
如果要使用宏:
这仅在板条箱功能部件rt
和macros
上受支持
#[tokio::main(flavor=“current#u thread”)]
异步fn main()->结果{
一些函数()正在等待
}
async fn some_function()->结果{
好(())
}
如果您需要指定的确切语法(这不是“使用Tokio轮询未来的最小功能集”),则运行时错误将指导您:
默认的运行时风格是multi_-thread
,但是rt-multi-thread
功能被禁用
#[tokio::main]
异步fn main()->结果{
一些函数()正在等待
}
async fn some_function()->结果{
好(())
}
另见:
rt
上受支持
fn main()->结果{
tokio::runtime::Builder::新建当前线程()
.build()
.unwrap()
.block_on(某些函数())
}
async fn some_function()->结果{
好(())
}
如果要使用宏:
这仅在板条箱功能部件rt
和macros
上受支持
#[tokio::main(flavor=“current#u thread”)]
异步fn main()->结果{
一些函数()正在等待
}
async fn some_function()->结果{
好(())
}
如果您需要指定的确切语法(这不是“使用Tokio轮询未来的最小功能集”),则运行时错误将指导您:
默认的运行时风格是multi_-thread
,但是rt-multi-thread
功能被禁用
#[tokio::main]
异步fn main()->结果{
一些函数()正在等待
}
async fn some_function()->结果{
好(())
}
另见:
main`函数不允许是'async
。为了使#[tokio::main]
工作,是否可以再导入一个东西?@Gatonito要使用tokio::main
属性,您需要“macros”
功能,默认运行时是多线程的,因此您需要“rt multi-thread”
而不是“rt”
。谢谢。我以前试过,我认为这是不可能的,因为我得到的main`函数不允许是'async
。为了使#[tokio::main]
工作,是否可以再导入一个东西?@Gatonito要使用tokio::main
属性,您需要“macros”
功能,默认运行时是多线程的,因此您需要“rt multi thread”
而不是“rt”
。
full = [
"fs",
"io-util",
"io-std",
"macros",
"net",
"parking_lot",
"process",
"rt",
"rt-multi-thread",
"signal",
"sync",
"time",
]
[dependencies]
tokio = { version = "1.4.0", features = ["rt"] }
[dependencies]
tokio = { version = "1.4.0", features = ["rt", "macros"] }
[dependencies]
tokio = { version = "1.4.0", features = ["rt", "rt-multi-thread", "macros"] }