Node.js什么时候阻止? [英] When is Node.js blocking?

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

问题描述

我已经使用Node.js一段时间了,我才意识到它可能会阻塞.我只是无法绕开Node.js被阻塞的条件而绞尽脑汁.

I have used Node.js for a while now and I just realized it can be blocking. I just cannot wrap my brain around the conditions under which Node.js becomes blocking.

  • 因此,Node.js是单线程的,因为(i)Javascript和(ii) 避免了所有的多线程陷阱.
  • 尽管是单线程的,但一次还是可以做很多事情 实现异步执行.因此,与数据库(I/O 一般而言)是非阻塞的(因为它是异步的).
  • 但是,所有传入的请求都需要做一些工作(即与数据库对话),并且 该工作的所有结果必须退还给客户(即发送 一些数据),他们使用那个单线程.
  • Node.js在单个线程内使用事件循环"来获取所有 请求并将其分配给非阻塞I/O任务.
  • So, Node.js is single-threaded because (i) Javascript is and (ii) avoids all the multi-threaded pitfalls.
  • To do a lot of things at once, despite being single-threaded, it implements asynchronous execution. So, talking with the DB (the I/O in general) is non-blocking (because it is asynchronous).
  • But, all the incoming requests to do some work (i.e. talk with the DB) and all the results of that work that must go back to the client (i.e. send some data) they use that single thread.
  • Node.js uses the "event loop" inside that single thread to get all the requests and assign them to non-blocking I/O tasks.

因此,由于异步回调,I/O任务是非阻塞的,但是单个线程却可能是阻塞的,因为它是同步的,并且由于同时显示了许多复杂的请求,因此事件循环可能会被阻塞?

So the I/O tasks are non-blocking because of asynchronous callbacks, but the single thread can be blocking, because it's synchronous and because the event loop can be jammed because a lot of complicated requests showing up at the same time?

  1. 我说对了,我理解正确吗?我,我猜不是 此处此处他们强调节点是单线程的 这意味着您的代码都不会并行运行."这实际上是什么 是什么意思以及它如何使Node阻塞?
  2. 因此,事件循环将永远运行并始终搜索请求,或者 发现新请求后开始执行?
  3. 节点阻塞弱点是否会使节点无用了? 项目,使其最终仅适用于微型网站和 小项目?
  1. Am I right, did I understand this correctly? I, guess I don't because here and here they emphasize that "Node is single-threaded which means none of your code runs in parallel". What does that actually mean and how does it make Node blocking?
  2. So, the event loop runs forever and always searches for requests, or it starts execution after it spots a new request?
  3. Does the Node blocking weakness renders Node useless for big projects and make it eventually suitable for only micro-sites and small projects?

非常感谢.

推荐答案

首先要明确:node.js整体上不是 单线程.节点确实通过libuv拥有一个线程池,它用于执行某些任务,这些任务当前在大多数平台上无法通过单个线程有效地完成(例如文件I/O),或者本质上是计算密集型的(例如zlib).应该注意的是,大多数crypto模块(本质上也将是计算密集型)目前没有异步/非阻塞接口(crypto.randomBytes()除外).

First, to be clear: node.js as a whole isn't single-threaded. Node does have a thread pool via libuv that it uses to perform some tasks that are either currently impossible to do efficiently from a single thread on most platforms (e.g. file I/O) or they are inherently computation intensive (e.g. zlib). It should be noted that most of the crypto module (which would also be inherently computation intensive) currently does not have an async/non-blocking interface (except for crypto.randomBytes()).

v8还利用多个线程来执行垃圾收集,功能优化等工作.

v8 also utilizes multiple threads to do things like garbage collection, optimization of functions, etc.

但是,节点中的几乎所有其他内容确实都发生在同一个单线程中.

However just about everything else in node does occur within the same, single thread.

现在要专门解决您的问题:

Now to address your questions specifically:

  1. javascript代码是从单个线程运行的事实并不会导致节点阻塞.正如此答案解释的那样,节点最重要的是(I/O)并发性,而不是(代码)并行性.您可以通过使用内置的cluster模块例如在多核/cpu系统上并行运行节点代码,但是节点的主要目标是能够处理大量I/并发,而无需为每个套接字/服务器/等分配一个线程.

  1. The fact that the javascript code is ran from a single thread doesn't make node block. As this answer explains, node is foremost about (I/O) concurrency rather than (code) parallelism. You could run node code in parallel by utilizing the built-in cluster module for example on a multi-core/cpu system, but node's primary goal is to be able to handle a lot of I/O concurrently without dedicating one thread per socket/server/etc.

有一个很好的详细文章

There is a good, detailed writeup here that describes how the event loop in node works.

前面所述的Node的主要目标是非常好地处理I/O,它适合大多数Web应用程序用例和任何类型的网络程序.

Node's primary goal as previously described is to handle I/O really well, which fits with the majority of use cases for web applications and any kind of network programs for example.

如果您的脚本受CPU限制(例如,您正在计算pi或对音频/视频进行代码转换),则最好将工作委托给子进程中的子进程(例如,调用ffmpeg进行代码转换,而不是使用javascript或在节点主线程上的c ++节点插件中同步执行此操作).如果您不同时进行其他任何操作(例如处理HTTP请求),则可以 进行这些阻止进程中的操作.有很多人会以这种方式使用节点来执行各种I/O并发性不那么重要的实用程序任务.这样的一个示例可能是执行js和css文件的缩小,整理和/或捆绑的脚本,或者是从大量图像中创建缩略图的脚本.

If your script is CPU-bound (e.g. you're calculating pi or transcoding audio/video), you are probably better off delegating that work to a child process in node (e.g. calling out to ffmpeg for transcoding instead of doing it in javascript or synchronously in a c++ node addon on node's main thread). You could do these blocking things in-process if you aren't doing anything else at the same time (like handling HTTP requests). There are many people who will use node in this way for performing various utility tasks where I/O concurrency isn't as important. One example of this might be a script that performs minification, linting, and/or bundling of js and css files or a script that creates thumbnails from a large set of images.

但是,如果您的脚本创建了一个TCP或HTTP服务器,例如从数据库中提取信息,对其进行格式化,然后将其发送回用户,则node将很擅长这样做,因为大部分时间都花在了上面在此过程中,只是在等待套接字/HTTP客户端发送(更多)数据,并在等待数据库用查询结果答复.

However, if your script instead creates a TCP or HTTP server for example that pulls information from a database, formats it, and sends it back to the user, then node will be good at doing that because the majority of the time spent in the process is just waiting for sockets/HTTP clients to send (more) data and waiting for the database to reply with results from queries.

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

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