我正在用這個例子學習 rust 中的期貨和執行緒,但這不能編譯,我不明白為什么。
use futures::future::Future;
use futures_cpupool::{CpuFuture, CpuPool};
use notify::{RecursiveMode, Watcher, watcher};
use std::{thread, time};
fn main() {
let pool = CpuPool::new(10);
let mut handles = Vec::new();
for i in 0..10 {
let i = i.clone();
let handle: CpuFuture<(), ()> = pool.spawn_fn(move || {
loop {
println!("{}", i);
}
Ok(())
});
handles.push(handle);
}
thread::sleep(time::Duration::from_secs(2));
for handle in handles {
handle.wait().unwrap();
}
}
錯誤:
error[E0599]: no method named `wait` found for struct `CpuFuture` in the current scope
--> src/main.rs:28:17
|
28 | handle.wait().unwrap();
| ^^^^ method not found in `CpuFuture<(), ()>`
|
::: /home/placek/.cargo/registry/src/github.com-1ecc6299db9ec823/futures-0.1.31/src/future/mod.rs:297:8
|
297 | fn wait(self) -> result::Result<Self::Item, Self::Error>
| ---- the method is available for `CpuFuture<(), ()>` here
|
= help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
|
2 | use futures::future::Future;
|
我有Future范圍,所以我認為它應該可以作業,我還檢查了 CpuFuture 實作了Future特征。
這是我的 cargo.toml:
[package]
name = "folderWatcher"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
notify = "4.0.12"
futures-cpupool = "0.1.7"
futures = "0.3.25"
另外我想問一下是否需要明確啟動每個未來?
uj5u.com熱心網友回復:
futures-cpupool需要futuresversion 0.1,并且 version0.3.25與它不兼容。所以降級期貨:
futures = "0.1"
然而,正如@isaactfa 所說,真正的問題是futures-cpupool太老了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/525224.html
標籤:多线程锈异步等待
上一篇:在腳本中實作多執行緒/并行處理
