Node.js child process to Python process

≡放荡痞女 提交于 2019-12-10 12:14:41

问题


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:


回答1:


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.)



来源:https://stackoverflow.com/questions/54713963/node-js-child-process-to-python-process

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