这是一个示例程序:

extern crate futures;
extern crate tokio_core;

use futures::{Async, Future, Stream};
use tokio_core::reactor::Core;
use tokio_core::net::TcpListener;

fn main() {
    let mut core = Core::new().unwrap();

    futures::sync::oneshot::spawn(
        TcpListener::bind(&"127.0.0.1:5000".parse().unwrap(), &core.handle())
            .unwrap()
            .incoming()
            .for_each(|_| {
                println!("connection received");
                Ok(())
            }),
        &core,
    );

    let ft = futures::future::poll_fn::<(), (), _>(|| {
        std::thread::sleep_ms(50);
        Ok(Async::NotReady)
    });

    core.run(ft);
}

如您所见,我调用 oneshot::spawn 然后立即删除其返回值,理论上应该取消包含在其中的 future 。但是,当我运行这个程序然后连接到 127.0.0.1:5000 时,它​​仍然打印“收到连接”。它为什么这样做?我希望它不会打印任何内容并删除 TcpListener ,从端口解除绑定(bind)。

最佳答案

这是一个(现已修复) bug in the futures crate0.1.18 版本应该包括修复。

它在 keep_running: bool/SpawnHandle 中使用了 Executor 的反转值。

关于rust - 为什么不删除这个 SpawnHandle 取消它的 future ?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48359296/

10-15 23:33