我想執行這段執行python腳本的節點js代碼。通過這段代碼工作正常。但是「前進」和「結束」的響應立即顯示在前端。一旦python腳本的執行完成,就必須顯示「finshed」。通過nodejs執行python腳本
app.post('/execute', function(request, response){
response.write("running");
console.log("executing")
var pyshell = new PythonShell('./python_codes/test.py')
pyshell.on('message', function (message) {console.log(message);});
pyshell.end(function (err) {if (err){throw err;};console.log('finished');});
response.write("finished");
response.end();
});
'PythonShell'內使用你的反應'child_process.spawn',這是異步的 - 你在這裏說「快跑這條巨蟒的事情,當告訴我它是通過使用'.end'給出的回調完成的,而當你這樣做時,我將繼續使用我自己的東西。「我懷疑你需要等待'pyshell.terminated'來顯示正確的值,或者將響應傳遞給'.end'回調 –
@SimonFraser完整的代碼未顯示,因此HTTP庫是否支持這是可能的(甚至可能),一旦'app.post'回調完成,'response.end()'將被隱式調用。 –
@FredrickBrennan公平點:)我比節點更蟒蛇,所以我試圖去看看PythonShell的東西了 –