Thursday 15 July 2010

javascript - How do I realize One Input One Output in Nodejs with the module child_process -



javascript - How do I realize One Input One Output in Nodejs with the module child_process -

i seek create r , nodejs communicate each other, run problems. explain more chunk of code

var cp = require('child_process') var spawn = cp.spawn var r = spawn("rterm",["--ess" ,"--slave"]) r.stdout.on('data',function(data){ console.log(data.tostring()) })

i send command kid process, kid process generates lot of output(within reasonable length), output automatically read nodejs because of 'data' listener.

but kid process, output 1 chunk, when read nodejs, becomes 2 or more chunks sometimes. , not wanted.

how prepare in code?

when spawn child process, stdout readable stream. when attach 'data' event, stream goes 'flowing mode', meaning homecoming info available. when have lot of data, homecoming info 1 chunk @ time.

there's couple of ways prepare :

you buffer chunks, , output them when 'end' event fired, below :

var output=""; r.stdout.on('data',function(data){ output+=data }).on('end',function(){ console.log(output); });

or, can remove info event listener altogether , phone call stdout.read().

stdout.read() read content of stream @ moment, must phone call when process won't output anymore.

javascript node.js child-process

No comments:

Post a Comment