使用Node.js中的spawn运行Python文件

huangapple go评论105阅读模式
英文:

Running python file using spawn in node js

问题

这是我的server.js文件:

const path = require("path");
const fs = require("fs");
const express = require("express");
const multer = require("multer");
const cors = require("cors");
const app = express();
const { spawn } = require("child_process");
const { request } = require("http");
const { response } = require("express");
const { stderr } = require("process");

function denoiseVideoByRaw(req, res) {
  var spawn = require("child_process").spawn;
  // 原始音频
  var process = spawn("python", ["denoise_video.py"], { shell: true });
  process.stdout.on("data", function async(data) {
    // res.send(data.toString());
    console.log(data.toString());
  });
}
app.post("/denoise-video-by-raw", denoiseVideoByRaw);

app.listen(8000, () => {
  console.log("server is running on localhost:8000");
});

请注意,我已经将HTML实体编码(")替换为双引号("),以便您的代码在运行时不会出现问题。如果您需要进一步的帮助,请告诉我。

英文:

I am trying to run python file on a post request from client(react js). Everything was working fine on the other windows 10 system. But when I tried to use it on another computer, now it is not working. On terminal, it is showing no error.

This is my server.js file:



const path = require("path");
const fs = require("fs");
const express = require("express");
const multer = require("multer");
const cors = require("cors");
const app = express();
const { spawn } = require("child_process");
const { request } = require("http");
const { response } = require("express");
const { stderr } = require("process");


function denoiseVideoByRaw(req, res) {
  
  var spawn = require("child_process").spawn;

  // raw audio
  var process = spawn("python", ["denoise_video.py"], {shell: true});

  process.stdout.on("data", function async(data) {
    // res.send(data.toString());
    console.log(data.toString());
  });
}
app.post("/denoise-video-by-raw", denoiseVideoByRaw);


app.listen(8000, () => {
  console.log("server is running on localhost:8000");
});

答案1

得分: 0

根据Node.js的官方文档,你还需要捕获stderr

请应用以下更改:

var result = '';
process.stdout.on('data', function (data) { // 回调函数无需异步
    result += data.toString(); // 每个数据块都是一个 chunk
});

stderr:

process.stderr.on('data', function (data){
    result += data.toString();
});

最终结果:

process.stdout.on('end',function (data){
    console.log(result);
});
英文:

According to Node.js official documentation, you need to also capture stderr.

Please apply these changes:

var result = '';
process.stdout.on('data', function (data) { // The callback function no need to be as async
    result += data.toString(); // each data is a chunk
});

stderr:

process.stderr.on('data', function (data){
    result += data.toString();
});

final result:

process.stdout.on('end',function (data){
    console.log(result);
});

huangapple
  • 本文由 发表于 2023年1月4日 04:39:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/74998444.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定