actor、reactor与proactor模型

时间:2022-05-25 06:25:32

actor、reactor与proactor模型:高性能服务器的几种模型概念。

actor模型:

实体之通过消息通讯,各自处理自己的数据,能够实现这并行。

说白了,有点像rpc。

skynet是actor模型。

reactor模型:

1 向事件分发器注册事件回调

2 事件发生

4 事件分发器调用之前注册的函数

4 在回调函数中读取数据,对数据进行后续处理

libevent是reactor模型。

proactor模型:

1 向事件分发器注册事件回调

2 事件发生

3 操作系统读取数据,并放入应用缓冲区,然后通知事件分发器

4 事件分发器调用之前注册的函数

5 在回调函数中对数据进行后续处理

ASIO是preactor模型。

reactor和proactor的主要区别是,前者应用在回调函数中读取数据,然后进行后续的数据处理;而后者数据读取有操作系统完成,回调函数制作数据处理。 Proactor是异步,Reactor是同步阻塞。

相同点:demultiplexor负责提交IO操作(异步)、查询设备是否可操作(同步),然后当条件满足时,就回调handler。
不同点:
  1. 异步情况下(Proactor),当回调handler时,表示IO操作已经完成;
  2. 同步情况下(Reactor),回调handler时,表示IO设备可以进行某个操作(can read or can write),handler这个时候开始提交操作。

摘抄一些关键的东西:

"
Two patterns that involve event demultiplexors are called Reactor and Proactor [1]. The Reactor patterns 
involve synchronous I/O, whereas the Proactor pattern involves asynchronous I/O.
"

关于两个模式的大致模型,从以下文字基本可以明白:

"
An example will help you understand the difference between Reactor and Proactor. We will focus on the read 
operation here, as the write implementation is similar. Here's a read in Reactor:

* An event handler declares interest in I/O events that indicate readiness for read on a particular socket ;
* The event demultiplexor waits for events ;
* An event comes in and wakes-up the demultiplexor, and the demultiplexor calls the appropriate handler; 
* The event handler performs the actual read operation, handles the data read, declares renewed interest in 
  I/O events, and returns control to the dispatcher .

By comparison, here is a read operation in Proactor (true async):

* A handler initiates an asynchronous read operation (note: the OS must support asynchronous I/O). In this 
  case, the handler does not care about I/O readiness events, but is instead registers interest in receiving 
  completion events;
* The event demultiplexor waits until the operation is completed ;
* While the event demultiplexor waits, the OS executes the read operation in a parallel kernel thread, puts 
  data into a user-defined buffer, and notifies the event demultiplexor that the read is complete ;
* The event demultiplexor calls the appropriate handler; 
* The event handler handles the data from user defined buffer, starts a new asynchronous operation, and returns
  control to the event demultiplexor.