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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With