简体   繁体   English

Node.js子进程到Python进程

[英]Node.js child process to Python process

I must send text from a node.js child process to a python process. 我必须将文本从node.js子进程发送到python进程。 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 而Python进程master.py

#!/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'); 我必须使用utf-8编码的输入阅读器,所以我使用的是sys.stdin ,但是似乎当node.js使用child.stdin.write(data + '\\r\\n');写入子进程stdin child.stdin.write(data + '\\r\\n'); , this will not be read by sys.stdin in for line in ifp: sys.stdin不会在sys.stdinfor line in ifp:读取此内容for line in ifp:

You'll need to call child.stdin.end() in the Node program after the final call to child.stdin.write() . 你需要调用child.stdin.end()在节点程序最后调用后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. 在调用end()之前, child.stdin可写流会将写入的数据保存在缓冲区中,因此Python程序将看不到它。 See the Buffering discussion in https://nodejs.org/docs/latest-v8.x/api/stream.html#stream_buffering for details. 有关详细信息,请参见https://nodejs.org/docs/latest-v8.x/api/stream.html#stream_buffering中的“ 缓冲”讨论。

(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.) (如果您向stdin写入大量数据,那么写缓冲区最终将填满到一个点,在该点处,所存储的数据将自动刷新到Python程序中。然后,该缓冲区将再次开始收集数据。需要end()调用以确保清除写入数据的最后部分。它还具有指示子进程不再在该流上发送任何数据的作用。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM