node.js spawn stdout string broken when received

别来无恙 提交于 2021-01-28 10:41:17

问题


I have the following python code (script.py):

import sys
import numpy as np
from scipy import stats

def generateBeta() :
    fit = (0.075252656968743836, 498.49505071718869, 9.9999999999999991e-05, 0.18136881492296397)
    rnd = stats.beta.rvs(fit[0], fit[1], fit[2], fit[3], 581)
    rndStr = ""
    for i in rnd:
            rndStr += str(i) + ','
    print rndStr[0:len(rndStr)-1]

if __name__ =='__main__' :
    generateBeta()

When running "python script.py" I get something like:

0.000461650100253,0.000100731728317,0.000106550237062,0.000168159408577,0.000167330103757,0.000100050650386,0.000127253399976,0.000100193300275,0.000101258755861,0.000115222086572,0.00010000230276, ....

All in one line

When I call it using node.js child_process#spawn in the following way:

var sys   = require('sys'),
    spawn = require('child_process').spawn,
    pyScript = spawn('python', ['./script.py']);
pyScript.stdout.setEncoding('utf8');
pyScript.stdout.on('data', function (data) {
  console.log(data);
});

The numbers get interrupted by something looking like a '\n' but is not. Somehow the stream is interrupted, i.e.

0.000461650100253,0.000100731728317,0.000106550237062,0.000168159408577,0.000167330103757,0.000100050650386,0. 000127253399976,0.000100193300275,0.000101258755861,0.000115222086572,0.00010000230276, ...

Which derives to an error on interpreting the data (i.e., causes 0.000127 later being interpreted as 127..)

Does anyone knows why such interruption occurs??

Thanks!!

Ariel.


回答1:


Per design, console.log writes a newline at the end of its output.

You probably want to use process.stdout.write instead.

http://nodejs.org/api/stdio.html#stdio_console_log_data http://nodejs.org/api/process.html#process_process_stdout




回答2:


As pointed above by adpalumbo, the event data is called for every "chunk". The problem is solved by concatenating data and taking action after the event 'close' occurs:

var allData = "";
   pwScript.stdout.on('data', function (data) {
      allData = allData.concat(data);
   });

pyScript.on('close', function () {
  allData.split(',').forEach(function(d) { process.stdout.write(d + '\n')});
});

adpalumbo, thanks for pointing this out!



来源:https://stackoverflow.com/questions/17936595/node-js-spawn-stdout-string-broken-when-received

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!