Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  9] [ 7]  / answers: 1 / hits: 8492  / 11 Years ago, wed, january 22, 2014, 12:00:00

I would like to separate server high consuming CPU task from user experience:



./main.js:



var express = require('express');
var Test = require('./resources/test');
var http = require('http');
var main = express();

main.set('port', process.env.PORT || 3000);
main.set('views', __dirname + '/views');
main.use(express.logger('dev'));
main.use(express.bodyParser());
main.use(main.router);

main.get('/resources/test/async', Test.testAsync);

main.configure('development', function() {
main.use(express.errorHandler());
});

http.createServer(main).listen(main.get('port'), function(){
console.log('Express server app listening on port ' + main.get('port'));
});


./resources/test.js:



function Test() {}
module.exports = Test;

Test.testAsync = function(req, res) {
res.send(200, Hello world, this should be sent inmediately);
process.nextTick(function() {
console.log(Simulating large task);
for (var j = 0; j < 1000000000; j++) {
// Simulate large loop
}
console.log(phhhew!! Finished!);
});
};


When requesting localhost:3000/resources/test/async I would expect the browser rendering Hello world, this should be sent inmediately really fast and node.js to continue processing, and after a while in console appearing finished message.



Instead, browser keeps waiting until node.js finishes large task and then renders the content. I've tried with res.set({ 'Connection': 'close' }); and also res.end(); but nothing works as expected. I've also googled with no luck.



How should it be to send the response to client immediately and server continue with tasks?



EDIT



posted fork method in solution


More From » node.js

 Answers
12

Thakns for Peter Lyons help, finally the main problem was firefox buffer: response was not so long as to flush it (so firefox kept waiting).



Anyway, for hight CPU performing tasks, node would keep hanged until finishing, so will not be attending new requests. If someone needs it, it can be achieved by forking (with child_process, see sample in http://nodejs.org/api/child_process.html)



Have to say that change of context by forking could take longer than splitting the task in different ticks.



./resources/test.js:



var child = require('child_process');
function Test() {}
module.exports = Test;

Test.testAsync = function(req, res) {
res.send(200, Hello world, this should be sent inmediately);
var childTask = child.fork('child.js');
childTask.send({ hello: 'world' });
};


./resources/child.js:



process.on('message', function(m) {
console.log('CHILD got message:', m);
});

[#48444] Tuesday, January 21, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lelasamiraa

Total Points: 208
Total Questions: 99
Total Answers: 107

Location: Uzbekistan
Member since Tue, Nov 30, 2021
3 Years ago
;