nodejs 是代表 Reactor 还是 Proactor 设计模式? [英] Is nodejs representing Reactor or Proactor design pattern?

查看:20
本文介绍了nodejs 是代表 Reactor 还是 Proactor 设计模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网上很多文章都展示了nodejs作为反应器模式的一个例子.不就是前摄吗?

Many articles online demonstrates nodejs as an example of reactor pattern. Isn't it rather proactor?

据我了解,两者的区别在于:

As far as I understand, the difference between the two is:

  1. reactor 在单线程(同步)中处理事件
  2. proactor 处理事件是具有完成回调的多线程(异步).

例如在这篇文章中:

Reactor Pattern 是 Node.js 中非阻塞 I/O 操作的一种想法.该模式提供了一个与每个 I/O 操作相关联的处理程序(在 Node.js 的情况下,一个回调函数).当一个 I/O 请求产生时,它被提交给一个解复用器.

Reactor Pattern is an idea of non-blocking I/O operations in Node.js. This pattern provides a handler(in case of Node.js, a callback function) that is associated with each I/O operation. When an I/O request is generated, it is submitted to a demultiplexer.

它实际上不是proactor的定义吗?

Isn't it actually definition of proactor?

推荐答案

我不熟悉 Proactor 设计模式.在阅读了一些关于它的内容后,我想我理解你的困惑.

I wasn't familiar with the Proactor design pattern. After reading a bit about it I think I understand your confusion.

网上很多文章以nodejs作为反应器模式的例子

Many articles online demonstrates nodejs as an example of reactor pattern

这是真的.

它实际上不是proactor的定义吗?

Isn't it actually definition of proactor?

这也是事实.

不同之处在于你的观点.

The difference is your point of view.

在内部,节点的事件循环是一个阻塞调用(具有讽刺意味).这只是使用非阻塞 I/O 的最有效方式.如果您感兴趣的事情发生,不同的操作系统具有不同的功能来请求操作系统唤醒您的进程.由于 POSIX 要求,有一个所有现代操作系统都支持的跨平台 API:select().Node.js 实际上使用 libuv,它会在编译时根据目标平台自动选择正确的 API.但就本回答而言,我们将重点关注 select().所以让我们看看 select():

Internally, node's event loop is a blocking call (ironically). That's just the most efficient way to use non-blocking I/O. Different OSes have different functions to request the OS to wake your process up if something you are interested in happens. Due to POSIX requirements there is a cross-platform API that all modern OSes support: select(). Node.js actually uses libuv which automatically picks the right API at compile time depending on the target platform. But for the purposes of this answer we're going to focus on select(). So lets look at select():

    numberOfEvents = select(numberOfWaits, read, write, err, timeout);

select() 功能块最多 timeout 毫秒,或者读取、写入或错误文件/套接字发生某些事情.只需一个函数,操作系统就提供了足够的功能来实现大多数 node.js,从诸如 setTimeout()setInterval() 之类的计时器到侦听网络套接字.使用 select() 事件循环看起来像这样:

The select() function blocks for up to timeout milliseconds or something happens to either the read, write or err files/sockets. With just a single function the OS provides enough functionality to implement most of node.js from timers like setTimeout() and setInterval() to listening to network sockets. Using select() the event loop looks something like this:

// Pseudocode:

while(1) {
    evaluateJavascript();
    timeout = calculateTimers();
    events = select(n, read, write, err, timeout);
    if (events > 0 || timersActive()) {
        getCallbacks(events, read, write, err, timers());
    }
}

这基本上是一个 Reactor 设计模式.

This is basically a Reactor design pattern.

然而,节点在其实现中隐藏了这一点.它向 Javascript 程序员公开的是一组 API,用于注册回调并在事件发生时调用这些回调.这部分是历史性的(浏览器 API 就是这样设计的),部分是实用的(它是一种更加灵活的架构 - 从 GTK 到 wxWindows 到 .Net 的几乎所有 GUI 框架都以这种方式工作).

However, node hides this away in its implementation. What it exposes to Javascript programmers is a set of APIs that registers callbacks and calls those callbacks when an event happens. This is partly historical (the browser APIs was designed that way) and partly practical (it's a much more flexible architecture - almost all GUI frameworks from GTK to wxWindows to .Net works this way).

您可能会意识到这听起来很像 Proactor 设计模式.事实上它是.

You may recognise that this sounds a lot like a Proactor design pattern. And in fact it is.

所以 node.js 本身就是 Reactor 设计模式的一个例子.

So node.js itself is an example of Reactor design pattern.

用 node.js 编写的 Javascript 程序是 Proactor 设计模式的例子.

Javascript programs written in node.js are examples of Proactor design pattern.

这篇关于nodejs 是代表 Reactor 还是 Proactor 设计模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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