Node.js 0.12.7 Windows上的writeFileSync和子进程
我的nodejs代码将http请求写入一个JSON文件,然后根据写入的JSON文件中的参数调用一个子进程来运行Node.js 0.12.7 Windows上的writeFileSync和子进程,node.js,Node.js,我的nodejs代码将http请求写入一个JSON文件,然后根据写入的JSON文件中的参数调用一个子进程来运行 app.post("/run_with_parameters", function(req, res){ fs.writeFileSync("parameters.json", JSON.stringify(req.body)); child_process.stdin.write("parameters.json"); //child process will r
app.post("/run_with_parameters", function(req, res){
fs.writeFileSync("parameters.json", JSON.stringify(req.body));
child_process.stdin.write("parameters.json"); //child process will read json and act accordingly
});
在Windows计算机上,“子进程”有时会获取旧参数而不是新参数,我怀疑这是因为fs.writeFileSync
在返回运行下一条语句之前没有完成对磁盘的写入。(根据)
这是Node.js错误/功能吗?或者它只存在于Windows计算机上
如果我这样编码会更好吗
app.post("/run_with_parameters", function(req, res){
fs.writeFile("parameters.json", JSON.stringify(req.body), function(){
child_process.stdin.write("parameters.json"); //child process will read json and act accordingly
});
});
这是否保证“子进程”总是得到更新的“parameters.json”?您链接到的文章(特别是在谈论ZFS时)表明这些文件操作都不能保证回调完成时写入数据。但是,流的事件表示:
调用end()方法并刷新所有数据时
将向基础系统发出此事件
因此,您可以在
finish
事件中使用、写入它并通过管道传输到您的子进程。如果我正确地解释了文档,您的文件就会在那里,并且包含内容。因为您使用的是writeFileSync,所以看到这个问题很奇怪。你确定它与多个请求的竞争条件无关吗?我支持竞争条件假设