【发布时间】: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