【问题标题】:Spawn child process from node to run python script returns 500从节点生成子进程以运行 python 脚本返回 500
【发布时间】:2017-03-16 05:40:06
【问题描述】:

我正在尝试生成一个子进程以从 Node.js 运行 python 脚本。我有以下请求:

/webcrawler?source=http://www.pygamers.com&method=BFS&nodeCount=3&depth=0&keyword=game

我已验证我的参数输入正确。这是我为处理app.js 中的请求而设置的代码:

app.get('/webcrawler', function(req, res){
  var python = require('child_process').spawn(
  'python',
  ["WebCrawler/Webcrawler.py"
  , req.query.source
  , req.query.method
  , req.query.nodeCount
  , req.query.depth
  , req.query.keyword]
  );
  var output = "";
  python.stdout.on('data', function(data){ output += data });
  python.on('close', function(code){
    if (code !== 0) {
        return res.send(500, code);
    }
    return res.send(200, output);
  });
});

我正在调用我的 Python 脚本 Webcrawler.py,它位于 WebCrawler 目录中。 WebCrawler 目录与app.js 在同一目录中。

但是,这个请求给了我 500,我无法弄清楚原因。看来我必须错误地生成子进程。我使用this answer 作为这样做的模型。

【问题讨论】:

  • 要么尝试删除 Webcrawler 作为您指示节点的路径的一部分,要么给它一个绝对路径是我的猜测 - 但我不太了解节点。了解 500 的含义可能也很有用。
  • 检查您的 python 脚本是否以您的目标退出代码退出。否则检查也作为结果参数传递给关闭事件的信号。对这两个参数进行进一步分析,以检查您的代码有效性。见nodejs.org/api/child_process.html#child_process_event_close

标签: python node.js child-process


【解决方案1】:

需要是绝对路径,比如/home/username/Webcrawler/webcrawler.py

【讨论】:

    【解决方案2】:

    听起来像是路径问题。 还结帐python-shell 包。让您的生活更轻松。

    【讨论】:

      猜你喜欢
      • 2017-11-08
      • 1970-01-01
      • 2013-12-08
      • 2011-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      相关资源
      最近更新 更多