我正在构建一个定期发出 HTTP 请求的服务。我tokio::timer::Delay
用作定期触发器和超级来进行 HTTP 调用。
一起使用它们会给我以下错误:
thread 'tokio-runtime-worker-1' panicked at 'cannot recursively call into `Core`', libcore/option.rs:960:5
我怎样才能使这项工作?
以下是该服务的简化版本。
main.rs
extern crate futures;
extern crate hyper;
extern crate tokio;
extern crate tokio_core;
extern crate tokio_timer;
use futures::{Future, Stream};
use hyper::Client;
use tokio_core::reactor::Core;
use std::time::{Duration, Instant};
use tokio::timer::Delay;
use std::io::{self, Write};
fn main() {
let when = Instant::now() + Duration::from_secs(1);
tokio::run({
Delay::new(when)
.map_err(|e| panic!("timer failed; err={:?}", e))
.and_then(move |_| {
let mut core = Core::new().unwrap();
let client = Client::new(&core.handle());
let uri = "http://httpbin.org/ip".parse().unwrap();
let work = 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(work).unwrap();
Ok(())
})
})
}
货运.toml
[dependencies]
futures = "0.1"
hyper = "0.11"
tokio-core = "0.1"
tokio-timer = "0.1"
tokio = "0.1"
serde = "1.0.19"
serde_derive = "1.0.19"
serde_json = "1.0.19"
hyper-tls = "0.1.3"