Node.js / express:立即响应客户端请求并在nextTick中继续执行任务 [英] Node.js / express: respond immediately to client request and continue tasks in nextTick
问题描述
我想将服务器消耗大的CPU任务与用户体验分开:
I would like to separate server high consuming CPU task from user experience:
./ main.js:
./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:
./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!");
});
};
当请求localhost:3000 / resources / test / async时,我希望浏览器呈现Hello世界,这应该立即发送真的很快和node.js继续处理,并在控制台出现完成消息一段时间后。
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.
相反,浏览器一直在等待直到node.js完成大任务然后呈现内容。我试过 res.set({'Connection':'close'});
以及 res.end();
但没有按预期工作。我也用Google搜索没有运气。
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?
编辑
在解决方案中发布了fork方法
posted fork method in solution
推荐答案
Thakns为Peter Lyons提供帮助,最后主要的问题是firefox缓冲区:响应不是很长时间(因此firefox一直在等待)。
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).
无论如何,对于高CPU执行任务,节点将一直挂起直到完成,因此不会参加新的请求。如果有人需要它,可以通过分叉来实现(使用child_process,请参阅 http://nodejs.org中的示例/api/child_process.html )
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:
./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:
./resources/child.js:
process.on('message', function(m) {
console.log('CHILD got message:', m);
});
这篇关于Node.js / express:立即响应客户端请求并在nextTick中继续执行任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!