Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor methods blocking even with this.unblock(). Have I hit a limit in concurrent Fibers?

Tags:

meteor

this is my code: server.js

var fs = Npm.require('fs');
Meteor.methods({
    "test":function(a){
      this.unblock();
      if(a==1){
         //NVIDIA is a test file and size is 40 M ,
         //Make the call spend more time to watch block happen
          var data = fs.readFileSync("/home/ec/download/NVIDIA");
          for(var i=0;i<5;i++){
            fs.writeFileSync("/home/ec/download/test/NVIDIA"+i, data);
          }
      }
      console.log(a);   
      return a;
    }
});

client.js

Meteor.call("test",1);
Meteor.call("test",2);

the result is :

1
2

the second call is blocked and the new Fibers is not created. Any idea about this ? thanks!

like image 986
L.T Avatar asked Jan 12 '23 11:01

L.T


1 Answers

I think this is happening because you are using fs.writeFileSync, which is blocking on a lower level than Fibers has access to.

Node waits on a blocking File IO operation which is still blocking with fibers unless you use it asynchronously.

If you use the callback style writeFile instead:

var readFile = Meteor._wrapAsync(fs.readFile.bind(fs));
var writeFile = Meteor._wrapAsync(fs.writeFile.bind(fs));

var data = readFile("/home/ec/download/NVIDIA");

for(var i=0;i<5;i++){
    writeFile("/home/ec/download/test/NVIDIA"+i, data);
}

Then this would bring up the writeFile and readFile methods up to the event loop where fibers has access to it.

Hopefully this should give you the behavior you expect.

like image 129
Tarang Avatar answered Jan 31 '23 07:01

Tarang