I have a slow future that blocks for 1 second before running to completion.
I've tried to use the join
combinator but the composite future my_app
executes the futures sequentially:
#![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);
}
Why does join
work like that? I expected the futures to be spawned on different threads.
What is the right way to obtain my goal?
Cargo.toml:
[dependencies]
futures-preview = "0.3.0-alfa.6"
Result:
$ time target/debug/futures03
real 0m2.004s
user 0m0.000s
sys 0m0.004s