我想生成一個子進程來從節點運行python腳本。我有以下要求:從節點生成子進程運行python腳本返回500
/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腳本是否以您的目標退出碼退出。否則,檢查也作爲結果參數傳遞給關閉事件的信號。用兩個參數做一些進一步的分析來檢查你的代碼的有效性。請參閱https://nodejs.org/api/child_process.html#child_process_event_close – DmiN