loretoparisi
loretoparisi

Reputation: 16271

Node.js child process to Python process

I must send text from a node.js child process to a python process. My dummy node client looks like

var resolve = require('path').resolve;
var spawn = require('child_process').spawn;

data = "lorem ipsum"

var child = spawn('master.py', []);

var res = '';
child.stdout.on('data', function (_data) {
    try {
        var data = Buffer.from(_data, 'utf-8').toString();
        res += data;
    } catch (error) {
        console.error(error);
    }
});
child.stdout.on('exit', function (_) {
    console.log("EXIT:", res);
});
child.stdout.on('end', function (_) {
    console.log("END:", res);
});
child.on('error', function (error) {
    console.error(error);
});

child.stdout.pipe(process.stdout);

child.stdin.setEncoding('utf-8');
child.stdin.write(data + '\r\n');

while the Python process master.py is

#!/usr/bin/env python

import sys
import codecs

if sys.version_info[0] >= 3:
    ifp = codecs.getreader('utf8')(sys.stdin.buffer)
else:
    ifp = codecs.getreader('utf8')(sys.stdin)

if sys.version_info[0] >= 3:
    ofp = codecs.getwriter('utf8')(sys.stdout.buffer)
else:
    ofp = codecs.getwriter('utf8')(sys.stdout)

for line in ifp:
    tline = "<<<<<" + line + ">>>>>"
    ofp.write(tline)

# close files
ifp.close()
ofp.close()

I must use a utf-8 encoded input reader so I'm using a sys.stdin, but it seems that when node.js writes to child process stdin using child.stdin.write(data + '\r\n');, this will not be read by sys.stdin in for line in ifp:

Upvotes: 1

Views: 1712

Answers (1)

ottomeister
ottomeister

Reputation: 5808

You'll need to call child.stdin.end() in the Node program after the final call to child.stdin.write(). Until end() is called, the child.stdin writable stream will hold the written data in a buffer, so the Python program won't see it. See the Buffering discussion in https://nodejs.org/docs/latest-v8.x/api/stream.html#stream_buffering for details.

(If you write lots of data into stdin then the write buffer will eventually fill to a point where the accumulated data will be flushed out automatically to the Python program. The buffer will then begin again to collect data. An end() call is needed to make sure that the final portion of the written data is flushed out. It also has the effect of indicating to the child process that no more data will be sent on this stream.)

Upvotes: 2

Related Questions