On Mon, Apr 29, 2013 at 4:13 PM, Afshin Mehrabani <[email protected]> wrote:
> Today when I tried to implement an example of using async/sync I/O methods
> in NodeJs, I faced an strange problem. When I'm trying to send requests with
> ab, I get this error in Async method:
>
>> { [Error: EMFILE, open 'sample.txt'] errno: 20, code: 'EMFILE', path:
>> 'sample.txt' }
>
>
> But the same functionality in Sync mode works well, without any errors.
>
> This is my ab command for running the test: ab -n 10000 -c 1000 -vhr
> http://localhost:8080/
>
> Here is my both codes:
>
> Async:
>
>> 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');
>
>
> Sync:
>
>> 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');
>
>
> What's the matter? Is there any problem in using Async methods?

There is in the way you use them.

The synchronous version is effectively serial, i.e. it processes
requests one by one (IOW, there is only one file descriptor at any
point in time that refers to sample.txt) and that's why you don't hit
the open file descriptor limit.  It also means that fs.readFileSync()
is now the single largest bottleneck in your application.

The asynchronous version runs concurrently, meaning there are 1,000
file descriptors - or however many simultaneous connections you're
serving - referring to sample.txt.  Raise `ulimit -n` and the problem
goes away (or cache the contents of sample.txt, of course.)

-- 
-- 
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to