主程序:
var http = require('http');
var url = require('url');
var cp = require('child_process');
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
if( pathname == '/wait' ) {
console.log("Will wait!");
cp.exec('node applications\\demo\\block.js', [], myCallback);
console.log(new Date());
}
else{
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('Hello!\n');
response.end();
}
console.log('Cool ' + (new Date()));
function myCallback(){
console.log("ok: " + (new Date()));
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('Thanks for waiting!\n');
response.end();
}
}
http.createServer(onRequest).listen(8081);
console.log('Server started');
exec调用的block.js是(即休眠10秒):
var startTime = new Date().getTime();
while (new Date().getTime() < startTime + 10000);
如果我访问http://localhost:8081/wait,会进入pathname == '/wait'的语句,并且通过exec调用block.js休眠10秒。这时我在休眠结束之前迅速的访问http://localhost:8081/,迅速返回结果。这个我认为是正常的。
但是如果我同时打开三个http://localhost:8081/wait链接,发现第一个链接大约花10秒,第二个链接大约花20秒,第三个链接大约花30秒(从console的输出看他们是串行的)。通过任务管理器看到只多了一个node进程(win 7操作系统,四核)。难道这三次访问exec不应该开三个block.js进程吗?请教该如何理解这段代码和这个现象?Nodejs所说的能同时处理多个请求到底该如何理解呢?
附上console输出:
Server started
Will wait!
Sun Jan 25 2015 00:59:50 GMT+0800 (中国标准时间)
Cool Sun Jan 25 2015 00:59:50 GMT+0800 (中国标准时间)
ok: Sun Jan 25 2015 01:00:01 GMT+0800 (中国标准时间)
Will wait!
Sun Jan 25 2015 01:00:21 GMT+0800 (中国标准时间)
Cool Sun Jan 25 2015 01:00:21 GMT+0800 (中国标准时间)
ok: Sun Jan 25 2015 01:00:31 GMT+0800 (中国标准时间)
Will wait!
Sun Jan 25 2015 01:00:31 GMT+0800 (中国标准时间)
Cool Sun Jan 25 2015 01:00:31 GMT+0800 (中国标准时间)
ok: Sun Jan 25 2015 01:00:41 GMT+0800 (中国标准时间)
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
ringa_lee