简体   繁体   English

Node.js非阻塞性质

[英]Node.js Non Blocking Nature

I am new to node.js and still trying to understand the philosophy behind it . 我是node.js的新手,仍在努力理解它背后的哲学。 As I learned , node.js runs in one process only as opposed to php which opens a process\\thread for each request. 据我所知,node.js只在一个进程中运行,而不是php,它为每个请求打开一个进程\\线程。 And while you can say Node is 'non-blocking' for i/o , it is blocking for requests(the requests pile up since there is no new thread for each new request) and theoretically if you've written a node.js app that doesn't deal fast with each request you get in trouble. 虽然你可以说Node对于i / o来说是“非阻塞的”,但它阻止了请求(由于每个新请求没有新线程,所以请求堆积起来)理论上如果你编写了一个node.js应用程序这对你遇到麻烦的每个请求都没有快速处理。

My question is this - how can I tell if a certain handling of a request takes too long so that it will block all other requests for too long and hinder the performance of my app ? 我的问题是 - 如何判断某个请求的处理时间是否过长,以至于它会阻止所有其他请求太长时间并阻碍我的应用程序的性能?

I know that all the 'heavy' actions on the server (db querying , file system searching) are done by callbacks and therefore cannot block node. 我知道服务器上的所有“重”动作(db查询,文件系统搜索)都是通过回调完成的,因此不能阻塞节点。 However what if all other operations that are done synchronously by the server to handle a request just take too long ? 但是,如果服务器同步完成的所有其他操作处理请求只需要太长时间呢?

For example- the server needs to write a lot of html to the response . 例如,服务器需要在响应中写入大量的html。 What happens then ? 那么会发生什么?

How can the node programmer know if he's doing too much with a certain request (in a blocking manner) , is it experience , intuition or are there clear guides on how to do it ? 节点程序员如何知道他是否对某个请求(以阻塞方式)做了太多,是经验,直觉还是有关于如何做到这一点的明确指南?

There are no clear guidelines as to where is the limit between synchronous code and asynchronous code, it is more of a matter with the application flow. 关于同步代码和异步代码之间的限制在哪里没有明确的指导,它更多地与应用程序流有关。 Asynchronous operations should be preferred since they allow Node.js main process to start handling other requests in the meantime. 异步操作应该是首选的,因为它们允许Node.js主进程在此期间开始处理其他请求。

That said, simply using callbacks for every function is not a solution since a piece of code as such: 也就是说,简单地为每个函数使用回调并不是一个解决方案,因为一段代码就是这样的:

function sum(a, b, callback){
   var sum = a + b;
   callback(sum);
}

sum(2,3, function(sum){
   console.log(sum);
}

Is still synchronous. 仍然是同步的。 To make it asynchronous process.nextTick could be used as such: 为了使它成为异步process.nextTick可以这样使用:

function sum(a, b, callback){
   var sum = a + b;
   process.nextTick(function(){
     callback(sum);
   });
}

sum(2,3, function(sum){
   console.log(sum);
}

The general rule of thumb is to avoid synchronous recursive calculations, heavy loops and IO operations. 一般的经验法则是避免同步递归计算,重循环和IO操作。

Finding out if a request takes too long and thus will hinder the performance can not be defined so generally as the limits are application specific. 找出请求是否花费太长时间并因此会妨碍性能不能定义,因为限制是特定于应用程序的。 Those request are located by running performance tests on the application. 通过对应用程序运行性能测试来定位这些请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM