2016-09-12 29 views
2

我需要從節點發送一個大的JSON文件到Python。我可以用spawn創建child_process來調用Python文件,但是我無法弄清楚如何將數據發送給它。我試過使用管道,但我不理解文檔。從NodeJS發送大的JSON字符串到Python

代碼:

var dataset = JSON.stringify(doc.data); 

// Call the python API 
try { 
    var py = require('child_process').spawn('python3', ['api.py', analysis, input]); 
} catch(error) { 
    console.log(error); 
} 

// Pipe the data to the Python module 
py.stdin.pipe(fs.createWriteStream(dataset)); 
py.stdin.end(); 

錯誤:

Uncaught Error: ENAMETOOLONG: name too long, open [file data printed to console here] 
+0

你應該設置一些聽衆對事件處理的流錯誤,並在流結束。 –

+0

http://lukeramsey.io/child – Hitmands

回答

2

Here是你正在嘗試做一個很好的指導!

好像你也應該做py.stdin.write('json data');

+0

完美,謝謝! –