账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    rust tokio中Future的使用疑惑
    • 2020-01-01 00:00
    • 11
    52
    0

    使用tokio时把hyper client发送get请求,为什么第一种方法可以直接返回,第二种方法却一直阻塞!

    方法一:

    //main方法
    
    tokio::run(lazy(|| {
            let url = String::from("http://www.baidu.com").parse::<hyper::Uri>().unwrap();
            let client = Client::new().get(url).map_err(|_| ProcessError::GetRequestError).and_then(|res| {
                println!("{}",res.status());
                return future::ok(());    
            }).map_err(|_| ());
            return client;
        }));

    第二种方法

    impl Future for DocumentBuilder {
        type Item = RcDom;
        type Error = ProcessError;
        fn poll(&mut self) -> Poll<Self::Item,Self::Error> {
            let response = self.client.get(self.uri.clone());
            let body = response.wait();
            if body.is_ok() {
                println!("is ok!");
            } else {
                println!("err");
            }
            panic!("..");
        }
    }
    tokio::run(lazy(|| {
            let document = DocumentBuilder::new("https://www.baidu.com").unwrap().map_err(|err| {
                println!("{:?}",err);
            }).map(|_| {
                println!("..");
            });
            return document;
        }));
    

    第二种方法为什么不能panic,而是一直wait

    1
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部