异步事件模型

原文: http://blog.chinaunix.net/uid-23242010-id-94816.html


   traffic server设计了一个基于事件驱动的多线程模型,通过EventSystem+Continuation完成了所有函数的异步调度。下图摘自apache提供的traffic server开发文档,给出了事件模型的结构图。
    EventSystem=EventProcessor+Event+Ethread。EventProcessor负责创建一个(组)线程Ethread,并分配给该(组)线程一个事件类型EventType。
    Continuation是一个被动的由事件驱动的状态机。一个Continuation执行的是一个逻辑上相对完整的功能。
    EventProcessor通过指定具体的EventType调度某个Continuation,根据EventType与Continuation构造一个Event,通过Event触发一个正确的Ethread进行异步调度执行该Continuation。
    TS启动时候已经创建了一些处理多种事件类型的Ethread,如专用于网络处理的线程,对应事件类型为ET_NET。默认事件类型为ET_CALL。目前,最新的ts2.1.5版本在启动时新增了一组事件类型为ET_TASK的线程,这组线程专门用于插件开发以及后台任务执行等作用。
    有三种方式调度一个Continuation:在将来的某个时间调度,在距当前某个时间以后调度,以及每隔一个时间段就执行一次Continuation,在EventProcessor中对应的三个函数为schedule_at,schedule_in, schedule_every。上图中将Continuation以sleeping作为修饰,意指Continuation需要Event事件唤醒执行。
    最后给出trafficserver的sdk开发文档中对异步事件模型的描述:
 
  1. Traffic Server is a multi-threaded process. There are two main reasons why a server might use multiple threads:
  2. To take advantage of the concurrency available with multiple CPUs and multiple I/O devices.
  3. To manage concurrency from having many simultaneous client connections. For example, a server could create one thread for each connection, allowing the operating system (OS) to control switching between threads.
  4. Traffic Server uses multiple threads for the first reason. However, Traffic Server does not use a separate OS thread per transaction because it would not be efficient when handling thousands of simultaneous connections.
  5. Instead, Traffic Server provides special event-driven mechanisms for efficiently scheduling work: the event system and continuations. The event system is used to schedule work to be done on threads. A continuation is a passive, event-driven state machine that can do some work until it reaches a waiting point; it then sleeps until it receives notification that conditions are right for doing more work. For example, HTTP state machines (which handle HTTP transactions) are implemented as continuations.
  6. Continuation objects are used throughout Traffic Server. Some might live for the duration of the Traffic Server process, while others are created (perhaps by other continuations) for specific needs and then destroyed. Traffic Server Internals (below) shows how the major components of Traffic Server interact. Traffic Server has several processors, such as cache processor and net processor, that consolidate cache or network I/O tasks. Processors talk to the event system and schedule work on threads. An executing thread calls back a continuation by sending it an event. When a continuation receives an event, it wakes up, does some work, and either destroys itself or goes back to sleep & waits for the next event.
 
    更加详细的描述请查看千石的blog

### 计算机网络中异步事件模型的概念和实现方式 #### 1. 异步事件模型的概念 异步事件模型是一种基于事件驱动的编程模型,它允许程序在等待某些操作完成时继续执行其他任务。这种模型的核心思想是通过事件通知机制来避免线程阻塞,从而提高系统资源的利用率和程序运行效率。与同步IO模型相比,异步事件模型不会让线程停留在等待状态,而是通过回调函数或事件处理器的方式,在事件发生时通知程序进行相应的处理[^1]。 #### 2. 异步事件模型的实现方式 异步事件模型的实现通常依赖于操作系统提供的IO多路复用机制以及异步IO支持。以下是常见的实现方式: - **IO多路复用**:`select`、`poll` 和 `epoll` 是常见的IO多路复用机制,它们允许一个线程同时监视多个文件描述符的状态变化。尽管这些机制本质上属于同步IO范畴,但它们可以通过事件轮询的方式减少线程阻塞时间,从而间接支持异步事件模型的实现[^4]。 ```c int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout); ``` - **异步IO (AIO)**:真正的异步IO由操作系统提供支持,当IO操作完成后,内核会主动通知应用程序,而无需应用程序主动轮询或阻塞等待。这种方式可以显著降低CPU消耗并提高并发性能[^4]。 - **事件循环(Event Loop)**:许多现代框架(如Node.js、libevent、libuv)使用事件循环来实现异步事件模型事件循环是一个无限循环,它不断检查事件队列中的事件,并调用相应的事件处理器。以下是一个简单的伪代码示例: ```python while True: event = get_next_event() # 从事件队列中获取下一个事件 if event: handle_event(event) # 调用事件处理器 ``` - **回调函数与Future/Promise模式**:在异步编程中,回调函数用于处理异步操作的结果。例如,在JavaScript中,Promise对象可以用来更优雅地管理异步操作的链式调用[^2]。 #### 3. 异步事件模型的原理 异步事件模型的工作原理可以概括为以下几个关键点: - **事件注册**:应用程序向事件循环注册感兴趣的事件类型(如读就绪、写就绪、超时等)。 - **事件检测**:事件循环通过IO多路复用机制或内核通知机制检测事件的发生。 - **事件通知**:当某个事件发生时,事件循环将该事件加入事件队列。 - **事件处理**:事件循环从队列中取出事件,并调用相应的事件处理器进行处理。 #### 4. 异步事件模型的优点 - 提高并发能力:通过避免线程阻塞,异步事件模型可以在单线程环境中处理大量并发连接。 - 减少资源消耗:相比于多线程模型异步事件模型不需要为每个任务创建独立的线程,从而降低了内存和上下文切换开销[^1]。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值