Stream 如何每N秒从一个无界队列中提取消息,并将它们生成到Tokio处理程序中?

Stream 如何每N秒从一个无界队列中提取消息,并将它们生成到Tokio处理程序中?,stream,rust,future,rust-tokio,Stream,Rust,Future,Rust Tokio,我试图每隔N秒从一个无限队列中提取消息(它们本身就是未来),并将它们生成到Tokio处理程序中 我尝试了几十种变体,但似乎找不到正确的方法。看起来这应该是可能的,但我总是遇到未来类型不匹配的问题,或者最终导致借债问题 这是或多或少显示我想要的代码: let fut = Interval::new_interval(Duration::from_secs(1)) .for_each(|num| vantage_dequeuer.into_future() ) .fo

我试图每隔N秒从一个无限队列中提取消息(它们本身就是未来),并将它们生成到Tokio处理程序中

我尝试了几十种变体,但似乎找不到正确的方法。看起来这应该是可能的,但我总是遇到未来类型不匹配的问题,或者最终导致借债问题

这是或多或少显示我想要的代码:

let fut = Interval::new_interval(Duration::from_secs(1))
        .for_each(|num| vantage_dequeuer.into_future() )
        .for_each(|message:VantageMessage |{
            handle.spawn(message);
            return Ok(());
        })
        .map_err(|e| panic!("delay errored; err={:?}", e));

core.run(fut);
完整代码:

extern crate futures; // 0.1.24
extern crate tokio; // 0.1.8
extern crate tokio_core; // 0.1.17

use futures::future::ok;
use futures::sync::mpsc;
use futures::{Future, Stream};
use std::thread;
use std::time::Duration;
use tokio::timer::Interval;
use tokio_core::reactor::Core;

type VantageMessage = Box<Future<Item = (), Error = ()> + Send>;

fn main() {
    let (enqueuer, dequeuer) = mpsc::unbounded();
    let new_fut: VantageMessage = Box::new(ok(()).and_then(|_| {
        println!("Message!");
        return Ok(());
    }));
    enqueuer.unbounded_send(new_fut);
    let joinHandle = worker(Some(dequeuer));
    joinHandle.join();
}

/*
    Every second extract one message from dequeuer (or wait if not available)
    and spawn it in the core
*/
fn worker(
    mut vantage_dequeuer: Option<mpsc::UnboundedReceiver<VantageMessage>>,
) -> thread::JoinHandle<()> {
    let dequeuer = dequeuer.take().unwrap();
    let joinHandle = thread::spawn(|| {
        let mut core = Core::new().unwrap();
        let handle = core.handle();
        let fut = Interval::new_interval(Duration::from_secs(1))
            .for_each(|num| vantage_dequeuer.into_future())
            .for_each(|message: VantageMessage| {
                handle.spawn(message);
                return Ok(());
            })
            .map_err(|e| panic!("delay errored; err={:?}", e));

        core.run(fut);
        println!("Returned!");
    });
    return joinHandle;
}
extern板条箱期货;//0.1.24
东京外部板条箱;//0.1.8
外部板条箱东京大学核心;//0.1.17
使用futures::future::ok;
使用futures::sync::mpsc;
使用未来:{Future,Stream};
使用std::线程;
使用std::time::Duration;
使用tokio::timer::Interval;
使用tokio_堆芯::反应堆::堆芯;
类型VantageMessage=Box;
fn main(){
let(enqueuer,dequeuer)=mpsc::unbounded();
让new_fut:VantageMessage=Box::new(ok(())。然后(| | |{
println!(“消息!”);
返回Ok(());
}));
排队器.无限发送(new\u fut);
让joinHandle=worker(Some(dequeuer));
joinHandle.join();
}
/*
每秒从出列器中提取一条消息(如果不可用,请等待)
并在核心产生它
*/
fn工人(
mut vantage_dequeuer:选项,
)->thread::JoinHandle{
让dequeuer=dequeuer.take().unwrap();
让joinHandle=thread::spawn(| |{
让mut core=core::new().unwrap();
让handle=core.handle();
设fut=Interval::new_Interval(持续时间::from_secs(1))
.for|u each(|num|vantage|u dequeuer.into_future())
.for|u each(|消息:VantageMessage |{
handle.spawn(消息);
返回Ok(());
})
.map|u err(| e | panic!(“延迟出错;错误={:?}”,e));
核心运行(fut);
println!(“返回!”);
});
返回接头手柄;
}

错误[E0425]:在此作用域中找不到值'dequeuer'
-->src/main.rs:33:20
|
33 |让dequeuer=dequeuer.take().unwrap();
|^^^^^^^^^在此范围内找不到
错误[E0599]:找不到类型“std::option::option>>:futures::Stream”的名为“into_future”的方法`

Interval
UnboundedReceiver
都是流,因此我将使用它们组合:

压缩流等待两个流生成一个项目,然后返回该对。如果发生错误,则会立即返回该错误。如果任一流结束,则压缩流也将结束


请检查如何创建一个,然后检查您的问题以将其包括在内。我们无法说出代码中存在哪些板条箱、类型、特征、字段等。例如,
dequeuer
不是定义的变量。试着制造一些能再现你在网络上的错误的东西,或者你可以在一个全新的货运项目中再现它。还有。惯用的Rust使用
snake\u case
表示变量、方法、宏和字段<对于类型,代码>大写;对于静力学和常数,
SNAKE\u案例
。请改用
join\u handle
。在块的末尾使用
return
也不是惯用方法,只需让最后一行是表达式,从而是块的值。太棒了!我看了看链子,眼睛瞎得看不见拉链。我想关闭反应堆,不管侧面的产卵是否完成,所以core.run对我来说更好。如果我没有错的话,因为流完成后每个都会出错,有没有办法避免错误?我希望能够做core.run(future.expect)(“管道中的错误”);这将始终触发for_迭代行为的每一端。@ArkaitzJimenez for_每次都将在流完成时出错-你为什么这么说?我没见过。你能提供导致这个错误的代码吗?看起来这个错误是我以前的impl的一个产物,它可以完美地工作。
extern crate futures; // 0.1.24
extern crate tokio;   // 0.1.8
extern crate tokio_core; // 0.1.17

use futures::{
    future::ok,
    sync::mpsc,
    {Future, Stream},
};
use std::{thread, time::Duration};
use tokio::timer::Interval;
use tokio_core::reactor::Core;

type VantageMessage = Box<Future<Item = (), Error = ()> + Send>;

pub fn main() {
    let (tx, rx) = mpsc::unbounded();

    let new_fut: VantageMessage = Box::new(ok(()).and_then(|_| {
        println!("Message!");
        Ok(())
    }));
    tx.unbounded_send(new_fut).expect("Unable to send");
    drop(tx); // Close the sending side

    worker(rx).join().expect("Thread had a panic");
}

fn worker(queue: mpsc::UnboundedReceiver<VantageMessage>) -> thread::JoinHandle<()> {
    thread::spawn(|| {
        let mut core = Core::new().unwrap();
        let handle = core.handle();

        core.run({
            Interval::new_interval(Duration::from_secs(1))
                .map_err(|e| panic!("delay errored; err={}", e))
                .zip(queue)
                .for_each(|(_, message)| {
                    handle.spawn(message);
                    Ok(())
                })
        })
        .expect("Unable to run reactor");
        println!("Returned!");
    })
}
fn worker(queue: mpsc::UnboundedReceiver<VantageMessage>) -> thread::JoinHandle<()> {
    thread::spawn(|| {
        tokio::run({
            Interval::new_interval(Duration::from_secs(1))
                .map_err(|e| panic!("delay errored; err={}", e))
                .zip(queue)
                .for_each(|(_, message)| {
                    tokio::spawn(message);
                    Ok(())
                })
        });
        println!("Returned!");
    })
}