你如何使用执行者来解决生锈中的未来?

问题描述 投票:0回答:1

这段代码恐慌:

extern crate futures;

use futures::Future;
use futures::future;
use futures::sync::oneshot::{channel, Canceled};
use std::thread;
use std::time::Duration;

fn maybe_oneday() -> Box<Future<Item = i32, Error = Canceled>> {
    let (s, r) = channel::<i32>();
    thread::spawn(move || {
        thread::sleep(Duration::from_millis(100));
        let _ = s.send(100);
    });
    return Box::new(r);
}

fn main() {
    let foo = maybe_oneday();
    let mut wrapper = foo.then(|x| {
        match x {
            Ok(v) => {
                println!("GOT: {:?}", v);
                future::ok::<i32, Canceled>(v)
            },
            Err(y) => {
                println!("Err: {:?}", y);
               future::err::<i32, Canceled>(y)
            }
        }
    });

    // wrapper.wait() <-- Works, but blocks
    let _ = wrapper.poll(); // <-- Panics
}

附:

thread 'main' panicked at 'no Task is currently running', /checkout/src/libcore/option.rs:891:5

据推测,我必须使用某种执行程序将任务解析委托给;但怎么样?

文档是指my_executor,但是there appear to be no implementations of this traitfind out more about executors链接被打破了?

我从哪里获得遗嘱执行人?

rust
1个回答
1
投票

通常,tokiofutures被设计为异步原语,而不是通用任务系统。

也就是说,如果你有多个任务想要异步发送并“发射并忘记”它们,请使用thread::spawn

如果你想在一个线程中运行多个任务,那么Future是用于在该线程中阻塞的正确原语,直到解决了一系列期货。

在这种情况下,我的问题确实没有意义,因为我认为Future应该代表类似于C#中的Task;也就是说,动态调度线程池以便稍后执行任务,并且可以在任务解决时将操作链接起来;这些任务又可能在不同的线程中执行。

这不是futurestokio支持的模型。

但是,我在这里补充说,只是为了激怒那些说话的人,我问的实际问题的答案是:

答案是tokio实现了许多基本的Executor,包括一个用于任意任务。

见:https://docs.rs/tokio/0.1.1/tokio/executor/current_thread/struct.TaskExecutor.html

具体来说:https://docs.rs/tokio/0.1.1/tokio/executor/current_thread/index.html

您可以像这样使用它们:

extern crate futures;
extern crate tokio;

use futures::Future;
use futures::future;
use futures::future::Executor;
use tokio::executor::current_thread;
use futures::sync::oneshot::{channel, Canceled};
use tokio::executor::current_thread::task_executor;
use std::thread;
use std::time::Duration;
use std::sync::mpsc::Sender;
use std::sync::mpsc;
use std::sync::{Arc, Mutex};

struct RemoteReactor {
    channel: Sender<Box<Future<Item=(), Error=()> + Send + 'static>>
}

impl RemoteReactor {
    fn new() -> RemoteReactor {
        let (send, recv) = mpsc::channel::<Box<Future<Item=(), Error=()> + Send + 'static>>();
        let threadsafe_recv = Arc::new(Mutex::new(recv));
        thread::spawn(move || {
            let reader = threadsafe_recv.lock().unwrap();
            current_thread::run(|_| {
                loop {
                    let future = reader.recv().unwrap();
                    println!("Got a future!");
                    task_executor().execute(future).unwrap();
                    break;
                }
            });
        });
        return RemoteReactor {
            channel: send
        };
    }

    fn execute(&self, future: Box<Future<Item=(), Error=()> + Send + 'static>) {
        self.channel.send(future).unwrap();
    }
}

fn maybe_oneday() -> Box<Future<Item=i32, Error=Canceled> + Send + 'static> {
    let (s, r) = channel::<i32>();
    thread::spawn(move || {
        thread::sleep(Duration::from_millis(100));
        let _ = s.send(100);
    });
    return Box::new(r);
}

fn main() {
    let foo = maybe_oneday();
    let wrapper = Box::new(foo.then(|x| {
        match x {
            Ok(v) => {
                println!("GOT: {:?}", v);
                future::ok::<(), ()>(())
            }
            Err(y) => {
                println!("Err: {:?}", y);
                future::err::<(), ()>(())
            }
        }
    }));

    let reactor = RemoteReactor::new();
    reactor.execute(wrapper);

    println!("Waiting for future to resolve");
    thread::sleep(Duration::from_millis(200));

    println!("All futures are probably resolved now");
}

NB。由于我不明白的原因,此代码不能在play.rust-lang.org(error[E0463]: can't find crate for tokio)上运行,但它确实会生锈1.24:

rustc 1.24.0 (4d90ac38c 2018-02-12)

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.1 secs
     Running `target\debug\hello_future.exe`
Waiting for future to resolve
Got a future!
GOT: 100
All futures are probably resolved now
© www.soinside.com 2019 - 2024. All rights reserved.