let mut core = tokio_core::reactor::Core::new().unwrap();
let handle = core.handle();
let client = Client::new(&handle);
let work = client.get(url).and_then(|res| {
println!("Response: {}", res.status());
println!("Headers: \n{}", res.headers());
res.body().for_each(|chunk| {
io::stdout().write_all(&chunk).map_err(From::from)
})
}).map(|_| {
println!("\n\nDone.");
});
core.run(work).unwrap();
Have a question regarding continuous 'work' for Client, currently, it is possible to create predefined work for the core as in the example above. Is there a way to listen on the channel or sth else to perform future work but still run core as well?
Certainly! For instance, here's how you could setup a thread to run a core foever, and receive messages to start new requests:
let (tx, rx) = futures::sync::mpsc::channel(0);
std::thread::spawn(move || {
let mut core = Core::new().unwrap();
let handle = core.handle();
let client = Client::new(&handle);
let messages = rx.for_each(|req| {
handle.spawn(client.request(req).and_then(do_something));
Ok(())
});
core.run(messages).unwrap();
});
// give the `tx` to someone else
tx.send(Request::new(Method::Get, uri))`
As this is a usage question, and not a bug/issue, I'm going to close this.
Most helpful comment
Certainly! For instance, here's how you could setup a thread to run a core foever, and receive messages to start new requests:
As this is a usage question, and not a bug/issue, I'm going to close this.