오늘 NodeJs에서 async/sync I/O 메소드를 사용하는 예제를 구현하려고 시도했지만 이상한 문제에 직면했습니다. 내가 ab
로 요청을 보내려고 할 때, 나는 비동기 방식이 오류가 발생합니다 :비동기 파일 읽기에서 EMFILE 오류가 발생했습니다.
{ [Error: EMFILE, open 'sample.txt'] errno: 20, code: 'EMFILE', path: 'sample.txt' }
그러나 동기화 모드에서 동일한 기능이 오류없이 잘 작동합니다.
테스트 실행에 대한 내 ab
명령이 있습니다 :
비동기 :
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');
동기화 : 여기
ab -n 10000 -c 1000 -vhr http://localhost:8080/
내 모두 코드입니다
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을 사용할 때 어떻게 링크를 닫을 수 있습니까? –
연결할 수를 제한하거나 사용중인 시스템의 열린 파일 제한을 늘려야 할 수 없습니다. – Mustafa