2
今天,當我嘗試實現在NodeJ中使用異步/同步I/O方法的示例時,我遇到了一個奇怪的問題。當我試圖用ab
發送請求,我在異步方法來獲取此錯誤:異步文件讀取中的EMFILE錯誤
{ [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');
這是怎麼回事?使用異步方法有什麼問題嗎?
很好,看起來不錯的解決方案。當我使用'fs.readFile'時,如何關閉鏈接? – 2013-04-25 12:25:56
您不能,您需要限制連接數或更好地增加您使用的系統的打開文件限制。 – Mustafa 2013-04-25 17:01:49