如何在Node.js中等待子进程完成? [英] How to wait for a child process to finish in Node.js?

查看:432
本文介绍了如何在Node.js中等待子进程完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

,但是Node不等待它完成。我该如何等待进程完成?

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

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

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

推荐答案

您应该使用exec-sync

You should use exec-sync

这可以让脚本等待您执行完了

That allow your script to wait that you exec is done

真正易于使用:

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

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

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

这篇关于如何在Node.js中等待子进程完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆