事件驱动模型和反应器模式有什么区别? [英] What is the difference between event driven model and reactor pattern?

查看:30
本文介绍了事件驱动模型和反应器模式有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自维基百科Reactor Pattern 文章:

反应器设计模式是一种事件处理模式,用于处理由一个或多个输入并发传递给服务处理程序的服务请求.

The reactor design pattern is an event handling pattern for handling service requests delivered concurrently to a service handler by one or more inputs.

它列举了一些例子,例如nodejstwistedeventmachine

It named a few examples, e.g. nodejs, twisted, eventmachine

但据我了解,上面是流行的事件驱动框架,所以让它们也成为反应器模式框架?

But what I understand that above is popular event driven framework, so make them also a reactor pattern framework?

如何区分这两者?或者它们是一样的?

How to differentiate between these two? Or they are the same?

推荐答案

反应器模式比事件驱动编程"更具体.它是在进行事件驱动编程时使用的特定实现技术.但是,该术语在典型对话中的使用并不准确,因此您应该谨慎使用它并期望您的听众理解您,并且在您遇到该术语时应谨慎解释该术语.

The reactor pattern is more specific than "event driven programming". It is a specific implementation technique used when doing event driven programming. However, the term is not used with much accuracy in typical conversation, so you should be careful about using it and expecting your audience to understand you, and you should be careful in how you interpret the term when you encounter its use.

查看反应器模式的一种方法是将其与非阻塞"操作的思想密切相关.当某些操作可以在不阻塞的情况下完成时,反应器会发出通知.例如,select(2) 可用于使用标准 BSD 套接字 API(recv(2)send(2) 等).select 会告诉您何时可以立即从套接字接收字节 - 例如,因为字节存在于该套接字的内核接收器缓冲区中.

One way to look at the reactor pattern is to consider it closely related to the idea of "non-blocking" operations. The reactor sends out notifications when certain operations can be completed without blocking. For example, select(2) can be used to implement the reactor pattern for reading from and writing to sockets using the standard BSD socket APIs (recv(2), send(2), etc). select will tell you when you can receive bytes from a socket instantly - because the bytes are present in the kernel receiver buffer for that socket, for example.

在考虑这些想法时您可能要考虑的另一种模式是 proactor 模式.与反应器模式相反,前摄器模式无论是否可以立即完成操作都会开始,让它们异步执行,然后安排发送完成通知.

Another pattern you might want to consider while thinking about these ideas is the proactor pattern. In contrast to the reactor pattern, the proactor pattern has operations start regardless of whether they can finish immediately or not, has them performed asynchronously, and then arranges to deliver notification about their completion.

Windows I/O 完成端口 (IOCP) API 是一个可以看到 proactor 模式的示例.在使用 IOCP 对套接字执行发送时,无论内核发送缓冲区中是否有空间用于该套接字,都会启动发送操作.发送操作继续(在另一个线程中,可能是内核中的一个线程),而 WSASend 调用立即完成.当发送实际完成时(意味着发送的字节已被复制到该套接字的内核发送缓冲区中),提供给WSASend调用的回调函数被调用(在应用程序的一个新线程中).

The Windows I/O Completion Ports (IOCP) API is one example where the proactor pattern can be seen. When performing a send on a socket with IOCP, the send operation is started regardless of whether there is any room in the kernel send buffer for that socket. The send operation continues (in another thread, perhaps a thread in the kernel) while the WSASend call completes immediately. When the send actually completes (meaning only that the bytes being sent have been copied into the kernel send buffer for that socket), a callback function supplied to the WSASend call is invoked (in a new thread in the application).

这种启动操作然后在完成时收到通知的方法是异步操作思想的核心.将其与 非阻塞 操作进行比较,在非阻塞操作中,您要等到操作可以立即完成后再尝试执行.

This approach of starting operations and then being notified when they are complete is central to the idea of asynchronous operations. Compare it to non-blocking operations where you wait until an operation can complete immediately before attempting to perform it.

任何一种方法都可用于事件驱动编程.使用反应器模式,程序等待(例如)套接字可读的事件,然后从中读取.使用前摄器模式,程序改为等待套接字读取完成的事件.

Either approach can be used for event driven programming. Using the reactor pattern, a program waits for the event of (for example) a socket being readable and then reads from it. Using the proactor pattern, the program instead waits for the event of a socket read completing.

严格来说,Twisted 误用了术语reactor.基于 select(2) (twisted.internet.selectreactor) 的 Twisted reactor 是使用非阻塞 I/O 实现的,非常类似于 reactor.但是,它向应用程序代码公开的接口是异步,使其更像前摄器.Twisted 也有一个基于 IOCP 的反应器.该反应器公开了相同的面向应用程序的异步 API 使用类似 proactor 的 IOCP API.这种混合方法在细节上因平台而异,使得术语反应器"和前摄器"都不是特别准确,但由于 twisted.internet.reactor 公开的 API 基本上是完全异步的对于非阻塞,proactor 可能是更好的名称选择.

Strictly speaking, Twisted misuses the term reactor. The Twisted reactor which is based on select(2) (twisted.internet.selectreactor) is implemented using non-blocking I/O, which is very reactor-like. However, the interface it exposes to application code is asynchronous, making it more proactor-like. Twisted also has a reactor based on IOCP. This reactor exposes the same asynchronous application-facing API and uses the proactor-like IOCP APIs. This hybrid approach, varying from platform to platform in its details, makes neither the term "reactor" nor "proactor" particularly accurate, but since the API exposed by twisted.internet.reactor is basically entirely asynchronous instead of non-blocking, proactor would probably have been a better choice of name.

这篇关于事件驱动模型和反应器模式有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆