我有多个线程执行一些繁重的操作,我需要在工作中使用客户端。我使用 Hyper v0.11 作为 HTTP 客户端,我想重用连接,所以我需要共享hyper::Client连接以保持打开连接(在keep-alive模式下)。
客户端不可在线程之间共享(它没有实现Sync或Send)。这是我尝试执行的代码的小片段:
let mut core = Core::new().expect("Create Client Event Loop");
let handle = core.handle();
let remote = core.remote();
let client = Client::new(&handle.clone());
thread::spawn(move || {
// intensive operations...
let response = &client.get("http://google.com".parse().unwrap()).and_then(|res| {
println!("Response: {}", res.status());
Ok(())
});
remote.clone().spawn(|_| {
response.map(|_| { () }).map_err(|_| { () })
});
// more intensive operations...
});
core.run(futures::future::empty::<(), ()>()).unwrap();
Run Code Online (Sandbox Code Playgroud)
此代码无法编译:
let mut core = Core::new().expect("Create Client Event Loop");
let handle = …Run Code Online (Sandbox Code Playgroud) 从现在到下一个午夜之间获得持续时间的惯用方法是什么?
我有这样的功能:
extern crate chrono;
use chrono::prelude::*;
use time;
fn duration_until_next_midnight() -> time::Duration {
let now = Local::now(); // Fri Dec 08 2017 23:00:00 GMT-0300 (-03)
// ... how to continue??
}
Run Code Online (Sandbox Code Playgroud)
它应该是Duration1小时,因为下一个午夜是2017年12月9日星期六00:00:00 GMT-0300(-03)