如何在 Node.js 中等待子进程完成?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22337446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:00:14  来源:igfitidea点击:

How to wait for a child process to finish in Node.js?

node.jschild-process

提问by David

I'm running a Python script through a child process in Node.js, like this:

我正在通过 Node.js 中的子进程运行 Python 脚本,如下所示:

require('child_process').exec('python celulas.py', function (error, stdout, stderr) {
    child.stdout.pipe(process.stdout);
});

but Node doesn't wait for it to finish. How can I wait for the process to finish?

但是 Node 不会等待它完成。我怎样才能等待该过程完成?

EDIT: Is it possible to do this by running the child process in a module I call from the main script?

编辑:是否可以通过在我从主脚本调用的模块中运行子进程来做到这一点?

采纳答案by Frederic Nault

You should use exec-sync

你应该使用 exec-sync

That allow your script to wait that you exec is done

这允许您的脚本等待您的 exec 完成

really easy to use:

非常容易使用:

var execSync = require('exec-sync');

var user = execSync('python celulas.py');

Take a look at: https://www.npmjs.org/package/exec-sync

看一看:https: //www.npmjs.org/package/exec-sync

回答by alex

Use exitevent for the child process.

exit为子进程使用事件。

var child = require('child_process').exec('python celulas.py')
child.stdout.pipe(process.stdout)
child.on('exit', function() {
  process.exit()
})

PS: It's not really a duplicate, since you don't want to use sync code unless you really really need it.

PS:它不是真正的重复,因为除非你真的需要它,否则你不想使用同步代码。

回答by Andrija J Fourkidney

NodeJS supports doing this synchronously. Use this:

NodeJS 支持同步执行此操作。用这个:

const exec = require("child_process").execSync;

var result = exec("python celulas.py");

// convert and show the output.
    console.log(result.toString("utf8");

Remember to convert the buffer into a string. Otherwise you'll just be left with hex code.

请记住将缓冲区转换为字符串。否则,您将只剩下十六进制代码。

回答by Kristofor Selden

You need to remove the listeners exec installs to add to the buffered stdout and stderr, even if you pass no callback it still buffers the output. Node will still exit the child process in the buffer is exceeded in this case.

您需要删除 exec 安装的侦听器以添加到缓冲的 stdout 和 stderr,即使您没有传递回调,它仍会缓冲输出。在这种情况下,节点仍然会退出缓冲区中的子进程。

var child = require('child_process').exec('python celulas.py');
child.stdout.removeAllListeners("data");
child.stderr.removeAllListeners("data");
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);

回答by TylersSN

In my opinion, the best way to handle this is by implementing an event emitter. When the first spawn finishes, emit an event that indicates that it is complete.

在我看来,处理这个问题的最好方法是实现一个事件发射器。当第一个 spawn 完成时,发出一个指示它已完成的事件。

const { spawn } = require('child_process');
const events = require('events');
const myEmitter = new events.EventEmitter();


firstSpawn = spawn('echo', ['hello']);
firstSpawn.on('exit'), (exitCode) => {
    if (parseInt(code) !== 0) {
        //Handle non-zero exit
    }
    myEmitter.emit('firstSpawn-finished');
}

myEmitter.on('firstSpawn-finished', () => {
    secondSpawn = spawn('echo', ['BYE!'])
})