简体   繁体   English

node.js实际上如何与事件循环一起使用?

[英]How node.js actually work with event loop?

I test the code 我测试代码

///create the web server
http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});
///
function compute() {
var x = 1111111112;
    while (x > 0) x--;//about 6 seconds
   setImmediate(compute);//then do it again
}
compute();

Let's say we have a task compute() which needs to run almost continuously, and does some CPU intensive calculations. 假设我们有一个任务compute(),它几乎需要连续运行,并进行一些CPU密集型计算。 If we wanted to also handle other events, like serving HTTP requests in the same Node process, I find it impossible to use process.nextTick() adjust CPU time on the JS Thread because process.nextTick's oberver in event loop is idle observer . 如果我们还想处理其他事件,例如在同一个Node进程中处理HTTP请求,我发现无法使用process.nextTick()来调整JS线程上的CPU时间,因为process.nextTick在事件循环中的观察者是空闲的Observer。 The observer priority is listed: 列出观察者优先级:

  1. idle observer //process.nextTick has the most highest priority 空闲的观察者//process.nextTick的优先级最高
  2. I/O observer // The second is I/O observer includes web request and other I/O operations I / O观察者// //第二个是I / O观察者,包括Web请求和其他I / O操作
  3. check observer // setImmediate() function's observer is check observer 检查观察者// setImmediate()函数的观察者是检查观察者

So I use setImmediate to make the whole compute cut into pieces of work so that other Observer(idle and I/O) could deal their events like a request before Javascript continue running compute() function. 因此,我使用setImmediate将整个计算分解为多个工作,以便其他观察者(空闲和I / O)可以像处理请求一样处理它们的事件,然后Javascript继续运行compute()函数。

The result is strange : When a request comes , it's dealed but not at one time.Since I set the compute()'s computing time at about 6 seconds, why my browser won't get the result until so many seconds pass(larger than 6 sec.) So I start to decrease the computing time at about 50 ms which is much more smaller than before. 结果很奇怪:当一个请求到来时,它就被处理了一次却没有被处理。由于我将compute()的计算时间设置为大约6秒,所以为什么我的浏览器要经过这么多秒才能得到结果(更大而不是6秒。)因此,我开始将计算时间减少到大约50毫秒,这比以前要小得多。

///create the web server
http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});
///
function compute() {
var x = 11111112;//I decrease this number for reducing the running time of compute(); 
    while (x > 0) x--;//about 50 ms
   setImmediate(compute);//then do it again
}
compute();

Then everything works quickly. 然后,一切都会很快进行。

What I worry is: the request should be dealed after 6 seconds in a whole operation and why it last for a long time? 我担心的是:在整个操作过程中,该请求应在6秒后得到处理,为什么它会持续很长时间? I always think the running model is like below: 我一直认为运行模型如下:

  1. Compute 6 seconds 计算6秒
  2. If Event loop find a I/O observer ( a web request) waiting, deal the request and response to the client browser 如果事件循环发现有一个I / O观察者(一个Web请求)在等待,请将请求和响应发送给客户端浏览器

  3. then event loop continue to do his work like compute() 然后事件循环继续执行自己的工作,例如compute()

  4. Loop above again 再次循环

But it seems node.js cut the request operation into many pieces which result in dealing the request with such a long time? 但是似乎node.js将请求操作切成许多片段,导致处理请求的时间这么长?

And am I wrong? 我错了吗? Any help will be appreciated. 任何帮助将不胜感激。

This is the fundamental concept of node.js. 这是node.js的基本概念。 Node.js is event based. Node.js是基于事件的。 Why is this so important? 为什么这个这么重要? Because it explains how Node can be asynchronous and have non-blocking I/O. 因为它说明了Node如何实现异步并具有非阻塞I / O。

Whenever task starts, it goes to the event loop queue then node.js interpreter sends the request to the event loop, event loop checks whether the task is I/O or non I/O blocking. 每当任务启动时,它都会进入事件循环队列,然后node.js解释器将请求发送到事件循环,事件循环检查任务是I / O阻塞还是非I / O阻塞。 If its I/O blocking request then event loop sends it to the thread pool which is managed by node.js library (libuv). 如果有其I / O阻止请求,则事件循环会将其发送到由node.js库(libuv)管理的线程池。 A thread pool manages a bunch of tasks like networking, DB operation, filesystem, and others. 线程池管理诸如网络,数据库操作,文件系统等任务。 Event loop always runs on the main thread because node.js is a single thread. 事件循环总是在主线程上运行,因为node.js是一个单线程。 All the I/O operations run on the thread pool. 所有I / O操作都在线程池上运行。 When task is completed in the thread poll it calls the callback and passes it to the queue again main thread pulls the request from the queue and process it at the same. 当任务在线程轮询中完成时,它将调用回调并将其再次传递到队列中,主线程从队列中拉出请求并对其进行处理。

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

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