<分区>
我有一个缓慢的 future ,在运行完成之前阻塞 1 秒。
我已尝试使用 join
组合器,但复合 future my_app
按顺序执行 future :
#![feature(pin, futures_api, arbitrary_self_types)]
extern crate futures; // v0.3
use futures::prelude::*;
use futures::task::Context;
use std::pin::PinMut;
use std::{thread, time};
use futures::executor::ThreadPoolBuilder;
struct SlowComputation {}
impl Future for SlowComputation {
type Output = ();
fn poll(self: PinMut<Self>, _cx: &mut Context) -> Poll<Self::Output> {
let millis = time::Duration::from_millis(1000);
thread::sleep(millis);
Poll::Ready(())
}
}
fn main() {
let fut1 = SlowComputation {};
let fut2 = SlowComputation {};
let my_app = fut1.join(fut2);
ThreadPoolBuilder::new()
.pool_size(5)
.create()
.expect("Failed to create threadpool")
.run(my_app);
}
为什么 join
会那样工作?我预计 future 会在不同的线程上产生。
实现目标的正确方法是什么?
cargo .toml:
[dependencies]
futures-preview = "0.3.0-alfa.6"
结果:
$ time target/debug/futures03
real 0m2.004s
user 0m0.000s
sys 0m0.004s