Одна основная концептуальная проблема, которую я вижу, состоит в том, что вы не должны создавать произвольные Core
s.Вы хотите поделиться ими как можно больше, потому что именно так Tokio обменивается данными между различными фьючерсами.
Создание единого ядра и использование его для HTTP-запроса и всей команды - правильная вещь.
гипер 0,11
гипер 0,11 не совместим с ящиком Токио .Вместо этого вам нужно использовать составляющие Tokio:
extern crate futures;
extern crate hyper;
extern crate tokio_core;
extern crate tokio_timer;
use futures::{Future, Stream};
use hyper::Client;
use std::{
io::{self, Write}, time::{Duration, Instant},
};
use tokio_core::reactor::Core;
use tokio_timer::Delay;
fn main() {
let when = Instant::now() + Duration::from_secs(1);
let mut core = Core::new().expect("Could not achieve criticality");
let handle = core.handle();
let command = Delay::new(when)
.map_err(|e| panic!("timer failed; err={:?}", e))
.and_then(move |_| {
let client = Client::new(&handle);
let uri = "http://httpbin.org/ip".parse().unwrap();
client.get(uri).and_then(|res| {
println!("Response: {}", res.status());
res.body()
.for_each(|chunk| io::stdout().write_all(&chunk).map_err(From::from))
})
});
core.run(command).expect("Meltdown occurred");
}
[dependencies]
futures = "0.1"
hyper = "0.11.27"
tokio-core = "0.1.17"
tokio-timer = "0.2.3"
гипер 0,12
При использовании гипер 0,12 это выглядит так:
extern crate hyper;
extern crate tokio;
use hyper::Client;
use std::{
error::Error, io::{self, Write}, time::{Duration, Instant},
};
use tokio::{
prelude::{Future, Stream}, timer::Delay,
};
type MyError = Box<Error + Send + Sync>;
fn main() {
let when = Instant::now() + Duration::from_secs(1);
let command = Delay::new(when).from_err::<MyError>().and_then(move |_| {
let client = Client::new();
let uri = "http://httpbin.org/ip".parse().unwrap();
client.get(uri).from_err::<MyError>().and_then(|res| {
println!("Response: {}", res.status());
res.into_body()
.from_err::<MyError>()
.for_each(|chunk| io::stdout().write_all(&chunk).map_err(From::from))
})
});
tokio::run(command.map_err(|e| panic!("Error: {}", e)));
}
[dependencies]
hyper = "0.12.0"
tokio = "0.1.6"