我已經查看了與此類似的其他問題,但它們對我不起作用。
我的問題是我這里有這個代碼:
function pyInput(){
const buffers = [];
proc.stdout.on('data', (chunk) => buffers.push(chunk));
proc.stdout.on('end', () => {
const result = JSON.parse(Buffer.concat(buffers));
console.log('Python process exited, result:', result);
});
proc.stdin.write(JSON.stringify([['a','b',1],['b','c',-6],['c','a',4],['b','d',5],['d','a', -10]]));
proc.stdin.end();
}
我試圖將其傳遞給的python函式:
def createGraph(listOfAttr):
for i in range(len(listOfAttr)):
G.add_edge(listOfAttr[i][0], listOfAttr[i][1], weight = listOfAttr[i][2])
#createGraph([['a','b',1],['b','c',-6],['c','a',4],['b','d',5],['d','a', -10]])
my_list = json.load(sys.stdin)
json.dump(my_list,sys.stdout)
該代碼基本上用于在圖中查找負回圈,我想從節點 js 加載該資料。然而,我的 python 程式從未完成執行,它只是卡住了,我不知道為什么。現在我不會將 Node 中的串列傳遞給 py 函式,但我試圖至少將它列印出來,看看它是否被傳遞給 python。
uj5u.com熱心網友回復:
sys.stdin 中的 json.load() 可能是問題所在。由于 sys.stdin 是一個管道,它在你告訴它之前永遠不會真正關閉,所以它只是占用流,等待新資料。您可能會占用流一段時間,input()直到您收到某種輸入字串,告訴您您到達終點,然后繼續前進。確保同時更新您的 node.js 腳本,將每條資料作為行提供,以 \n 終止,并在完成后發送您指定的結束信號。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/490923.html
標籤:Python 节点.js json python-3.x io
