extern crate futures; extern crate tokio; use futures::Future; use futures::sync::oneshot; const NUM_THREADS: usize = 2; const NUM_JOBS: u32 = 2; fn main() { let mut threadpool = tokio::executor::thread_pool::Builder::new(); threadpool.pool_size(NUM_THREADS); let mut runtime = tokio::runtime::Builder::new() .threadpool_builder(threadpool) .build() .unwrap(); // launch the main future runtime.spawn(futures::future::lazy(|| { run(); futures::future::ok(()) })); runtime.shutdown_on_idle().wait().unwrap(); } fn run() { for i in 0..NUM_JOBS { // launch some future work tokio::spawn(futures::future::lazy(move || { println!("starting {}", i); let id = do_some_future_work(i).wait().unwrap(); println!("finished {} -- {}", i, id); futures::future::ok(()) })); } } fn do_some_future_work(id: u32) -> oneshot::Receiver { let (tx, rx) = oneshot::channel::(); // launch some nested future work println!("spawning another future for {}", id); tokio::spawn(futures::future::lazy(move || { println!("sending response from the future for {}", id); tx.send(id).unwrap(); futures::future::ok(()) })); rx }