【发布时间】:2015-01-31 12:07:50
【问题描述】:
我正在阅读http://howtonode.org/understanding-process-next-tick 但是,它附带的代码并没有实现 CPU 密集型任务。
我尝试编写我的版本。但这是错误的。
compute() 执行后没有任何 IO 服务。
所以,我的问题是:在这种情况下使用 nextTick() 函数的正确方法是什么?
compute() 执行时我不想阻塞 IO。
var http = require('http');
function compute() {
// performs complicated calculations continuously
// ...
var result = 0;
for(var i = 0; i < 1000000; i++){
for(var j = i; j < 1000000; j++){
result += i + j;
}
}
process.nextTick(compute);
}
http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World');
}).listen(5000, '127.0.0.1');
compute();
【问题讨论】:
标签: javascript node.js