为什么 node.js 是异步的? [英] Why is node.js asynchronous?

查看:50
本文介绍了为什么 node.js 是异步的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上没有人问过这个问题(从我得到的所有建议"以及在我问这里之前的搜索).

Nobody has actually asked this (from all the 'suggestions' I'm getting and also from searching before I asked here).

那么为什么 node.js 是异步的?

So why is node.js asynchronous?

根据我经过一些研究得出的结论:

From what I have deduced after some research:

PHP 和 Python 等语言是脚本语言(我可能对脚本语言的实际语言有误)而 JavaScript 不是.(我想这是因为 JS 无法编译?)

Languages like PHP and Python are scripting languages (I could be wrong about the actual languages that are scripting languages) whilst JavaScript isn't. (I suppose this derives from the fact that JS doesn't compile?)

Node.js 在单线程上运行,而脚本语言使用多线程.

Node.js runs on a single thread whilst scripting languages use multiple threads.

异步意味着无状态,连接是持久的,而同步则(几乎)相反.

Asynchronous means stateless and that the connection is persistent whilst synchronous is the (almost) opposite.

也许答案可以在上述某处找到,但我仍然不确定.

Maybe the answer is found somewhere stated above, but I'm still not sure.

我与此主题相关的第二个也是最后一个问题是:

My second and last question related to this topic is this:

JavaScript 能否成为同步语言?

Could JavaScript be made into a synchronous language?

附注.我知道你们中的一些人会问为什么要使 JS 同步?"在你的答案中,但事实是我没有.我只是问这些类型的问题,因为我确信除了我自己之外,还有更多人考虑过这些问题.

PS. I know some of you will ask "why would you want to make JS synchronous?" in your answers, but the truth is that I don't. I'm just asking these types of questions because I'm sure there are more people out there than just myself that have thought about such questions.

推荐答案

Node.js 在单线程上运行,而脚本语言使用多线程.

Node.js runs on a single thread whilst scripting languages use multiple threads.

技术上不是.Node.js 使用多个线程,但只有一个执行线程.后台线程用于处理 IO 以使所有异步优点工作.有效地处理线程是一件非常痛苦的事情,因此下一个最佳选择是在事件循环中运行,这样代码就可以在后台线程被 IO 阻塞的情况下运行.

Not technically. Node.js uses several threads, but only one execution thread. The background threads are for dealing with IO to make all of the asynchronous goodness work. Dealing with threads efficiently is a royal pain, so the next best option is to run in an event loop so code can run while background threads are blocked on IO.

异步意味着无状态,连接是持久的,而同步则(几乎)相反.

Asynchronous means stateless and that the connection is persistent whilst synchronous is the (almost) opposite.

不一定.您可以非常轻松地在异步系统中保留状态.例如,在 Javascript 中,您可以使用 bind()this 绑定到函数,从而在函数返回时显式保留状态:

Not necessarily. You can preserve state in an asynchronous system pretty easily. For example, in Javascript, you can use bind() to bind a this to a function, thereby preserving state explicitly when the function returns:

function State() {
    // make sure that whenever doStuff is called it maintains its state
    this.doStuff = this.doStuff.bind(this);
}
State.prototype.doStuff = function () {
};

异步意味着不等待操作完成,而是注册一个监听器.这在其他语言中一直发生,尤其是任何需要接受用户输入的语言.例如,在 Java GUI 中,您不会阻止等待用户按下按钮,而是向 GUI 注册一个侦听器.

Asynchronous means not waiting for an operation to finish, but registering a listener instead. This happens all the time in other languages, notably anything that needs to accept input from the user. For example, in a Java GUI, you don't block waiting for the user to press a button, but you register a listener with the GUI.

我与此主题相关的第二个也是最后一个问题是:

My second and last question related to this topic is this:

JavaScript 能否成为同步语言?

Could JavaScript be made into a synchronous language?

从技术上讲,所有语言都是同步的,甚至 Javascript.然而,Javascript 在异步设计中工作得更好,因为它被设计为单线程.

Technically, all languages are synchronous, even Javascript. However, Javascript works a lot better in an asynchronous design because it was designed to be single threaded.

基本上有两种类型的程序:

Basically there are two types of programs:

  • 受 CPU 限制 - 让它跑得更快的唯一方法是获得更多的 CPU 时间
  • IO 限制 - 花费大量时间等待数据,因此更快的处理器无关紧要

视频游戏、数字处理器和编译器受 CPU 限制,而 Web 服务器和 GUI 通常受 IO 限制.Javascript 速度相对较慢(因为它有多复杂),因此它无法在 CPU 密集型场景中竞争(相信我,我已经编写了我对 CPU 密集型 Javascript 的公平份额).

Video games, number crunchers and compilers are CPU bound, whereas web servers and GUIs are generally IO bound. Javascript is relatively slow (because of how complex it is), so it wouldn't be able to compete in a CPU bound scenario (trust me, I've written my fair share of CPU-bound Javascript).

Javascript 不是根据类和对象进行编码,而是根据可以串在一起的简单函数进行编码.这在异步设计中非常有效,因为可以编写算法以在数据传入时增量处理.IO(尤其是网络 IO)非常慢,因此数据包之间有相当多的时间.

Instead of coding in terms of classes and objects, Javascript lends itself to coding in terms of simple functions that can be strung together. This works very well in asynchronous design, because algorithms can be written to process data incrementally as it comes in. IO (especially network IO) is very slow, so there's quite a bit of time between packets of data.

假设您有 1000 个实时连接,每个连接每毫秒传送一个数据包,处理每个数据包需要 1 微秒(非常合理).我们还假设每个连接发送 5 个数据包.

Let's suppose you have 1000 live connections, each delivering a packet every millisecond, and processing each packet takes 1 microsecond (very reasonable). Let's also assume each connection sends 5 packets.

在单线程、同步的应用程序中,每个连接都将被串行处理.花费的总时间为 (5*1 + 5*.001) * 1000 毫秒,或 ~5005 毫秒.

In a single-threaded, synchronous application, each connection will be handled in series. The total time taken is (5*1 + 5*.001) * 1000 milliseconds, or ~5005 milliseconds.

在单线程异步应用程序中,每个连接都将并行处理.由于每个数据包需要 1 毫秒,而处理每个数据包需要 0.001 毫秒,因此我们可以处理数据包之间的每个连接的数据包,因此我们的公式变为:1000*.001 + 5*1 毫秒,或 ~6 毫秒.

In a single-threaded, asynchronous application, each connection will be handled in parallel. Since every packet takes 1 millisecond, and processing each packet takes .001 milliseconds, we can process every connection's packet between packets, so our formula becomes: 1000*.001 + 5*1 milliseconds, or ~6 milliseconds.

这个问题的传统解决方案是创建更多线程.这解决了 IO 问题,但是当连接数增加时,内存使用量(线程消耗大量内存)和 CPU 使用量(将 100 个线程复用到 1 个核心上比 1 个线程在 1 个核心上更难)也随之增加.

The traditional solution to this problem was to create more threads. This solved the IO problem, but then when the number of connections rose, so did the memory usage (threads cost lots of memory) and CPU usage (multiplexing 100 threads onto 1 core is harder than 1 thread on 1 core).

但是,也有缺点.如果您的 Web 应用程序碰巧还需要进行一些繁重的数字运算,那么您就是 SOL,因为在处理数字时,连接需要等待.线程解决了这个问题,因为当数据准备好等待 IO 的线程时,操作系统可以换出 CPU 密集型任务.此外,node.js 绑定到单核,因此除非您启动多个实例和代理请求,否则您无法利用多核处理器.

However, there are downsides. If your web application happens to also need to do some heavy number crunching, you're SOL because while you're crunching numbers, connections need to wait. Threading solves this because the OS can swap out your CPU-intensive task when data is ready for a thread waiting on IO. Also, node.js is bound to a single core, so you can't take advantage of your multi-core processor unless you spin up multiple instances and proxy requests.

这篇关于为什么 node.js 是异步的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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