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. 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 . The observer priority is listed:
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.
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.
///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? I always think the running model is like below:
If Event loop find a I/O observer ( a web request) waiting, deal the request and response to the client browser
then event loop continue to do his work like compute()
But it seems node.js cut the request operation into many pieces which result in dealing the request with such a long time?
And am I wrong? Any help will be appreciated.
This is the fundamental concept of node.js. Node.js is event based. Why is this so important? Because it explains how Node can be asynchronous and have non-blocking 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. If its I/O blocking request then event loop sends it to the thread pool which is managed by node.js library (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. All the I/O operations run on the thread pool. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.