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

后端 未结 5 1359
终归单人心
终归单人心 2021-01-01 10:15

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, s         


        
5条回答
  •  南笙
    南笙 (楼主)
    2021-01-01 10:16

    NodeJS supports doing this synchronously. Use this:

    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.

提交回复
热议问题