Execution rust redis_async future without channels

I just want to execute redis_async future without any channels, only tokio::run(). Can somebody explain what is the main purpose of (tx, rx) channels and why i can't just

extern crate futures;
#[macro_use]
extern crate redis_async;
extern crate tokio;

use std::env;
use std::sync::Arc;

use futures::sync::oneshot;
use futures::{future, Future};

use redis_async::client;
fn main() {
    let test_f = client::paired_connect(&addr);
    let (tx, rx) = oneshot::channel();

    let send_data = test_f.and_then(|connection| {
        let key = "key".to_string();
        let data = "data".to_string();
        connection.send(resp_array!["SET", key, data])
    });
    tokio::run(send_data);
}

without sending my future to tx channel, if i don't need to share some data between threads?

Why is the channel there? Did you copy the code from somewhere?

Anyway, my guess is that oneshot channel is used as shutdown signal when the main future on the tokio loop is long running.

yeah, i just copy code block from here: https://github.com/benashford/redis-async-rs/blob/master/examples/realistic.rs and trying to change it to my needs, probably i don’t understand futures at all and official doc: https://tokio.rs/docs/getting-started/futures/ doesn’t help me :frowning:

That example is using the channel to communicate the result of those commands back to the caller once the futures are done. If you don’t need to do that, you don’t need the channel.

yeah, it is clear for me, but i don't understand why can't i just run my future send_data to tokio::run

I'm guessing by "can't run" you mean a compilation error. The reason is likely because the send_data is not a Future<Item = (), Error = ()>, which is what tokio::run wants (and why the lib example is using a channel to communicate the result back). In other words, it wants a future that doesn't return a value nor error. To that end, you can map() away the value and error:

let send_data = test_f.and_then(|connection| {
        let key = "key".to_string();
        let data = "data".to_string();
        connection.send(resp_array!["SET", key, data])
    })
    .map(|_| ())
    .map_err(|_| ());
    tokio::run(send_data);

You may want to print/log the error at least, however.

1 Like

thank you so much for your answers! :grinning: but it doesn't help me :frowning: maybe you know some simple topics about futures and channels for better understanding, it would really help me!

You mean to understand? Or it doesn't compile? Or it doesn't run properly?

The online guide, which you've already seen looks like, is the main documentation at the moment. There are various blogs and random articles on the internet as well, but I don't have any specific one to recommend. Tokio is undergoing a lot of changes at the moment, as well as Rust itself (e.g. async/await), that will change how you write tokio and futures code in the near future.

In the meantime, if you have more specific questions, we can try to help here.