等待所有子进程完成才能继续
Wait for all child processes to finish to continue
我想知道是否可以等待使用 spawn
函数创建的所有子进程完成后再继续执行。
我的代码如下所示:
const spawn = window.require('child_process').spawn;
let processes = [];
let thing = [];
// paths.length = 2
paths.forEach((path) => {
const pythonProcess = spawn("public/savefile.py", ['-d', '-j', '-p', path, tempfile]);
pythonProcess.on('exit', () => {
fs.readFile(tempfile, 'utf8', (err, data) => {
thing.push(...)
});
});
processes.push(pythonProcess);
});
console.log(processes) // Here we have 2 child processes
console.log(thing) // empty array.. the python processes didnt finish yet
return thing // of course it doesn't work. I want to wait for all the processes to have finished their callbacks to continue
如你所料,我想知道如何同时获取所有python脚本运行,并等待它们全部完成才能继续我的js代码.
我是运行节点10.15.3
谢谢
ForEach 将 Promise 推入 Promise 和 Promise.all()
的数组
我想知道是否可以等待使用 spawn
函数创建的所有子进程完成后再继续执行。
我的代码如下所示:
const spawn = window.require('child_process').spawn;
let processes = [];
let thing = [];
// paths.length = 2
paths.forEach((path) => {
const pythonProcess = spawn("public/savefile.py", ['-d', '-j', '-p', path, tempfile]);
pythonProcess.on('exit', () => {
fs.readFile(tempfile, 'utf8', (err, data) => {
thing.push(...)
});
});
processes.push(pythonProcess);
});
console.log(processes) // Here we have 2 child processes
console.log(thing) // empty array.. the python processes didnt finish yet
return thing // of course it doesn't work. I want to wait for all the processes to have finished their callbacks to continue
如你所料,我想知道如何同时获取所有python脚本运行,并等待它们全部完成才能继续我的js代码.
我是运行节点10.15.3
谢谢
ForEach 将 Promise 推入 Promise 和 Promise.all()
的数组