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

查看:262
本文介绍了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. 反应器在单个线程中(同步)处理事件
  2. proactor处理事件的原因是多个线程(异步)与完成回调.
  1. reactor handles events in a single thread (synchronously),
  2. proactor handles events is multiple threads (asynchronously) with completion callbacks.

例如,在本文中:

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的最有效方法.如果您感兴趣的事情发生,则不同的OS具有不同的功能来请求OS唤醒您的进程.由于POSIX的要求,所有现代OS都支持跨平台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毫秒,否则读取,写入或err文件/套接字会发生故障.操作系统仅具有一个功能,就提供了足够的功能来实现大多数的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注册回调并在事件发生时调用这些回调.这部分是历史性的(浏览器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天全站免登陆