Hyper: Question: continuous hyper client tokio core work

Created on 19 Jun 2017  路  1Comment  路  Source: hyperium/hyper

    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?

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:

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.

>All comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cactorium picture cactorium  路  26Comments

sfackler picture sfackler  路  49Comments

jswrenn picture jswrenn  路  44Comments

JosephShering picture JosephShering  路  22Comments

sfackler picture sfackler  路  23Comments