今日、NodeJ で async/sync I/O メソッドを使用する例を実装しようとしたとき、奇妙な問題に直面しました。でリクエストを送信しようとするとab
、非同期メソッドで次のエラーが発生します。
{ [Error: EMFILE, open 'sample.txt'] errno: 20, code: 'EMFILE', path: 'sample.txt' }
ただし、同期モードでの同じ機能は、エラーなしでうまく機能します。
これはab
、テストを実行するための私のコマンドです:
ab -n 10000 -c 1000 -vhr http://localhost:8080/
ここに私の両方のコードがあります:
非同期:
http.createServer(function (req, res) {
fs.readFile('sample.txt', function (err, data) {
if(err) {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end();
console.log(err);
} else {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(data);
}
});
}).listen(8080, '127.0.0.1');
同期:
http.createServer(function (req, res) {
var fileOutput = fs.readFileSync('sample.txt').toString();
if(!fileOutput) {
res.writeHead(500, {'Content-Type': 'text/plain'});
res.end('Error in reading the file.');
} else {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(fileOutput);
}
}).listen(8081, '127.0.0.1');
どうしたの?Async メソッドの使用に問題はありますか?