这段代码恐慌:
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 trait,find out more about executors链接已损坏?
我从哪里获得遗嘱执行人?
答案 0 :(得分:1)
通常,tokio
和futures
被设计为异步原语,而不是通用任务系统。
也就是说,如果您有多个任务,您希望异步发送,并且“发射并忘记”&#39;他们,使用thread::spawn
。
如果您有多个任务要在单个线程中运行,那么 在这种情况下,我的问题确实没有意义,因为我认为 这不是 但是,我在这里补充说,只是为了激怒那些说话的人,我问的实际问题的答案是: 答案是tokio实现了一些基本的 请参阅: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 您可以像这样使用它们: NB。此代码不会在play.rust-lang.org(Future
是用于在该线程中阻止直到解决一系列期货的正确原语。 / p>
Future
应该代表类似于C#中Task
的东西;也就是说,动态调度线程池以便稍后执行任务,并且可以在任务解决时将操作链接起来;而这些任务又可以在不同的线程中执行。futures
和tokio
支持的模型。Executor
,其中一个用于任意任务。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");
}
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