創建我有這個節點的js代碼多child_processes在節點JS
var bat = null;
app.post("/api/run", function(req,res) {
if(!bat) {
bat = spawn('cmd.exe', ['/c App.exe']);
}
if(req.body.success) {
bat.kill();
}
bat.stdin.write(req.body.input+'\n');
bat.stdout.on('data', function (data) {
console.log(data.toString());
res.end(JSON.stringify({data: data.toString()}));
});
bat.stderr.on('data', function (data) {
console.log(data.toString());
});
bat.on('exit', function (code) {
bat = null;
console.log('Child exited with code ' + code);
});
});
此代碼是假設只創建一個子進程將運行exe文件。但經過3 Ajax請求時,子進程殺掉這是控制檯輸出:
Input NO: 1 You entered: input 1
Input NO: 2 You entered: input 2
Input NO: 2 You entered: input 2
Input NO: 3 You entered: input 3
Input NO: 3 You entered: input 3
Input NO: 3 You entered: input 3
Child exited with code 1
Child exited with code 1
Child exited with code 1
而應該每輸入一次登錄,有應該只有一個子進程。這段代碼有什麼問題。
任何幫助將不勝感激。謝謝
非常感謝。你救了我的日子 –
@SaadMehmood不是所有英雄斗篷 – James