Javascript NodeJS:向持久子进程发送消息';没有人类互动的标准是什么? 要求

Javascript NodeJS:向持久子进程发送消息';没有人类互动的标准是什么? 要求,javascript,python,node.js,Javascript,Python,Node.js,我尝试将自定义消息发送到持久子进程的stdin。子进程不是节点进程,而是任意程序。子进程使用REPL交互提示接受用户输入并将结果打印到stdout,stdout通过管道传回父进程。我需要能够不断地向孩子发送信息,并获得一致的结果 我知道我们可以使用fork()将消息发送到基于NodeJS的子进程,但这不适用于非节点进程 尝试1:将父stdin管道化到子stdin 我最初的尝试是允许用户输入来自父进程stdin的消息,并通过管道将其传递给子进程。这是可行的,但最终不是我想要的 下面是父进程:hel

我尝试将自定义消息发送到持久子进程的stdin。子进程不是节点进程,而是任意程序。子进程使用REPL交互提示接受用户输入并将结果打印到stdout,stdout通过管道传回父进程。我需要能够不断地向孩子发送信息,并获得一致的结果

我知道我们可以使用
fork()
将消息发送到基于NodeJS的子进程,但这不适用于非节点进程

尝试1:将父stdin管道化到子stdin 我最初的尝试是允许用户输入来自父进程stdin的消息,并通过管道将其传递给子进程。这是可行的,但最终不是我想要的

下面是父进程:
hello\u childstdin.js
const{join}=require('path');
const{spawn}=require('child_进程');
const child=spawn('/usr/local/bin/python3',[join(u dirname,'hello_childstdin.py'));
工艺标准管道(子标准管道);
child.stdout.on('data',(data)=>{
log(`child stdout:\n${data}`);
});
child.stderr.on('data',(data)=>{
log(`child stderr:\n${data}`);
});
下面是子进程:
hello\u childstdin.py
为True时:
cmd=input('在此处输入命令(您好,再见,开始):')
打印('cmd:{}'。格式(cmd))
msg=cmd+':done\n'如果cmd在('hello','bye','do it')中,则为'else'未定义的cmd:{}'。格式(cmd)
将open('/path/to/hello_childstdin.txt','a')作为f:
f、 写入(msg)
打印('msg:{}'。格式(msg))
然而,我真正想要的是直接将消息发送到子进程的stdin,而无需人工干预

我尝试了以下方法,但失败了

尝试2:管道然后写入父标准输入。 父进程:
hello\u childstdin.js
const{join}=require('path');
const{spawn}=require('child_进程');
const child=spawn('/usr/local/bin/python3',[join(u dirname,'hello_childstdin.py'));
工艺标准管道(子标准管道);
//正在尝试写入父进程stdin
process.stdin.write('hello\n');
child.stdout.on('data',(data)=>{
log(`child stdout:\n${data}`);
});
child.stderr.on('data',(data)=>{
log(`child stderr:\n${data}`);
});
尝试3:写入子stdin。 父进程:
hello\u childstdin.js
const{join}=require('path');
const{spawn}=require('child_进程');
const child=spawn('/usr/local/bin/python3',[join(u dirname,'hello_childstdin.py'));
工艺标准管道(子标准管道);
//正在尝试写入父进程stdin
child.stdin.write('hello\n');
child.stdout.on('data',(data)=>{
log(`child stdout:\n${data}`);
});
child.stderr.on('data',(data)=>{
log(`child stderr:\n${data}`);
});
尝试4 查看child.stdin的文档解释:

如果子级正在等待读取其所有输入,它将不会继续 直到通过end()关闭此流

然后,我在我的父进程中尝试了以下操作

/。。。同上。。。
child.stdin.write('hello\n');
child.stdin.end();
// ... 同上。。。
这将结束子项,并且也不会将消息重写


与未分叉的子进程进行双工通信的正确方法是什么?

解决了这个问题:我可以将可读的流推送到子进程

。。。
const stream=require('stream');
...
var stdinStream=new stream.Readable();
stdinStream.push('hello\n');//将数据添加到内部队列,供流的用户使用
stdinStream.pipe(child.stdin);
多亏了