`Future::poll` 在返回 `NotReady` 后没有再次调用?

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

因为某些原因,我必须在我的项目中使用tokio 0.1。我想观看很多领事服务,我想使用轮询在一个线程中观看所有服务。但是我的演示代码似乎不起作用

货物.toml

[dependencies]
tokio = "0.1"
futures= "0.1"

运行测试用例,我得到的是:

running 1 test
this is a test : name1
this is a test : name2
test block_on_all::test_pool has been running for over 60 seconds

这是我的代码:


    use std::thread;
    use std::thread::sleep;
    
    use futures::{Async, Future, Stream};
    use futures::future::join_all;
    use tokio::runtime::current_thread;
    
    
    
    pub struct JnsWatcher {
        servicename: String,
    }
    
    impl JnsWatcher {
        pub fn new(name: String) -> Self {
            JnsWatcher {
                servicename
            }
        }
    }
    
    
    impl Future for JnsWatcher {
        type Item = ();
        type Error = ();
    
        fn poll(&mut self) -> Result<Async<Self::Item>, Self::Error> {
            loop {
                // sleep(std::time::Duration::from_secs(5));
                println!("this is a watch test : {}", self.servicename);
                return Ok(Async::NotReady);
            }
        }
    }
    
    
    #[test]
    fn test_pool() {
        let w = JnsWatcher::new("name1".to_string());
        let w2 = JnsWatcher::new("name2".to_string());
    
        let t = thread::Builder::new()
            .name("watchtest".to_string())
            .spawn(move || {
                current_thread::block_on_all(join_all(vec![w, w2])).unwrap();
            })
            .expect("fail to spawn worker thread");    
        t.join().unwrap();
        println!("test end");
    }

rust async-await future polling rust-tokio
© www.soinside.com 2019 - 2024. All rights reserved.