高并发中Poll和Epoll以及Future的概念是什么

发布时间:2021-10-11 09:30:07 作者:柒染
来源:亿速云 阅读:154

高并发中Poll和Epoll以及Future的概念是什么,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

高并发中有几个重要概念:Poll、Epoll、Future。

Future并不是一个主流的实现,但是Future与Poll的概念又是如此重要,我们必须放在开头来讲,因此这里先将重心放在Rust身上,由于Rust与Go、Java相比对于Future实现比较完整,特性支持也彻底。因此下面的代码均以Rust为例。

简单来讲Future不是一个值,而是一种值类型,一种在未来才能得到的值类型。Future对象必须实现Rust标准库中的std::future:: future接口。Future的输出Output是Future完成后才能生成的值。在Rust中Future通过管理器调用Future::poll来推动Future的运算。Future本质上是一个状态机,而且可以嵌套使用,我们来看一下面这个例子,在main函数中,我们实例化MainFuture并调用.await,而MainFuture除了在几个状态之间迁移以外,还会调用一个Delay的Future,从而实现Future的嵌套。

MainFuture以State0状态做为初始状态。当调度器调用poll方法时,MainFuture会尝试尽可能地提升其状态。如果future完成,则返回Poll::Ready,如果MainFuture没有完成,则是由于它等待的DelayFuture没有达到Ready状态,那么此时返回Pending。调度器收到Pending结果,会将这个MainFuture重新放回待调度的队列当中,稍后会再度调用Poll方法来推进Future的执行。具体如下:

use std::future::Future;
use std::pin::Pin;
usestd::task::{Context, Poll};
usestd::time::{Duration, Instant};
 
struct Delay {
    when: Instant,
}
impl Future forDelay {
    type Output = &'static str;
 
    fn poll(self: Pin<&mut Self>, cx:&mut Context<'_>)
        -> Poll<&'static str>
    {
        if Instant::now() >= self.when {
            println!("Hello world");
            Poll::Ready("done")
        } else {
          
            cx.waker().wake_by_ref();
            Poll::Pending
        }
    }
}
enum MainFuture {
   
    State0,
    State1(Delay),
    Terminated,
}
impl Future forMainFuture {
    type Output = ();
 
    fn poll(mut self: Pin<&mut Self>,cx: &mut Context<'_>)
        -> Poll<()>
    {
        use MainFuture::*;
     
        loop {
            match *self {
                State0 => {
                    let when = Instant::now() +
                        Duration::from_millis(1);
                    let future = Delay { when};
                    println!("initstatus");
                    *self = State1(future);
                }
                State1(ref mut my_future) =>{
                    matchPin::new(my_future).poll(cx) {
                        Poll::Ready(out) =>{
                            assert_eq!(out,"done");
                           println!("delay finished this future is ready");
                            *self = Terminated;
                            returnPoll::Ready(());
                        }
                        Poll::Pending => {
                            println!("notready");
                            returnPoll::Pending;
                        }
                    }
                }
                Terminated => {
                    panic!("future polledafter completion")
                }
            }
        }
    }
}
#[tokio::main]
async fn main() {
    let when = Instant::now() +Duration::from_millis(10);
  
    let mainFuture=MainFuture::State0;
    mainFuture.await;
  
}

关于高并发中Poll和Epoll以及Future的概念是什么问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

推荐阅读:
  1. 将poll程序改为epoll实现
  2. socket编程之 select、poll、kqueue、epoll

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

poll epoll future

上一篇:如何建立一个XMLHttpRequest对象池

下一篇:如何使用HTML5实现网页元素的拖放操作

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》