gpt4 book ai didi

即使使用 this.unblock() 也会阻塞 meteor 方法。我是否达到了并发 Fibers 的限制?

转载 作者:行者123 更新时间:2023-12-04 23:02:03 26 4
gpt4 key购买 nike

这是我的代码:
服务器.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;
}
});

客户端.js

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

结果是:

1
2

第二个调用被阻止,并且不会创建新的 Fibers。
对此有什么想法吗?谢谢!

最佳答案

我认为这是因为您正在使用 fs.writeFileSync ,它在比 Fibers 可以访问的级别更低的级别上进行阻塞。

节点等待阻塞文件 IO 操作,除非您异步使用它,否则该操作仍会被纤程阻塞。

如果您改用回调样式 writeFile:

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);
}

然后这会将 writeFile 和 readFile 方法带到事件循环中,其中纤维可以访问它。

希望这应该给你你期望的行为。

关于即使使用 this.unblock() 也会阻塞 meteor 方法。我是否达到了并发 Fibers 的限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21076423/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com