python - Python 子进程从 Node.js 父进程遍历标准输入
问题描述
我的Node.js父程序执行一个子Python进程并向其发送文本数据 - 请参阅此处了解实现。这工作正常,节点父进程将数据写入为:
child.stdin.setEncoding('utf-8');
child.stdin.write(data + '\r\n');
child.stdin.end();
Python 子进程回显它:
for line in sys.stdin:
ofp.write(line)
并将数据返回给父级:
child.stdout.on('data', function (_data) {
var data = Buffer.from(_data, 'utf-8').toString().trim();
res += data;
});
我希望我的父进程在子进程中分叉一次child.stdin.write();
,并在不关闭流的情况下进行后续调用。如果在父进程中我喜欢
setInterval(() => {
child.stdin.write(data + '\r\n');
child.stdin.end();
}, 1000);
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
由于end()
调用,这将在第二次执行时导致错误。如果我省略了,child.stdin.end();
我会得到任何输出。根据这里 sys.stdin.readline
将逐行阅读直到Ctrl+D
def read_stdin():
readline = sys.stdin.readline()
while readline:
yield readline
readline = sys.stdin.readline()
for line in read_stdin():
ofp.write(line)
另一种方法是
for line in iter(sys.stdin.readline, ''):
print line
在这两种情况下,我都没有将数据返回给父节点进程。
解决方案
推荐阅读
- python - 为我的 NMT 模型创建一个 python 模块
- sql - SQL语句,检查其他行是否具有相同的值
- javascript - Pangram 代码 - 无法理解我编写的代码中的问题 - 总是返回 false
- spring - Spring Boot Multitenancy - Hibernate - 在实体结构发生变化时使用 ddl-auto 更新所有模式
- spring - JPA - 使用 DDL 生成创建表时出错
- javascript - 根据 react-hooks 功能组件中的搜索结果更新子组件
- reactjs - useState 不重新渲染
- c++ - 无序映射的迭代器向量 - 编译错误
- tfs - TFS 服务器 2013 到 Azure DevOps 服务器 2020
- c++ - 试图故意造成内存泄漏,但程序结束后就消失了