gpt4 book ai didi

javascript - NodeJS : Send message to persistent child process's stdin without human interaction?

转载 作者:太空宇宙 更新时间:2023-11-03 23:47:44 27 4
gpt4 key购买 nike

要求

我尝试将自定义消息发送到持久子进程的标准输入。子进程不是 Node 进程,而是任意程序。子进程使用 REPL 交互式提示来接受用户输入并将结果打印到 stdout,然后通过管道返回到父进程。我需要能够不断向 child 发送消息并持续获得结果。

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

尝试 1:通过管道将父标准输入传输到子标准输入

我最初的尝试是允许用户从父进程的标准输入输入消息,并通过其子进程进行管道传输。这可行,但最终不完全是我想要的。

这是父进程:hello_childstdin.js

const {join} = require('path');
const {spawn} = require('child_process');

const child = spawn('/usr/local/bin/python3', [join(__dirname, 'hello_childstdin.py')]);

process.stdin.pipe(child.stdin);

child.stdout.on('data', (data) => {
console.log(`child stdout: \n${data}`);
});

child.stderr.on('data', (data) => {
console.log(`child stderr: \n${data}`);
});

这是子进程:hello_childstdin.py

while True:
cmd = input('Enter command here (hello, bye, do it):')
print('cmd: {}'.format(cmd))
msg = cmd+': done\n' if cmd in ('hello', 'bye', 'do it') else 'undefined cmd: {}'.format(cmd)
with open('/path/to/hello_childstdin.txt', 'a') as f:
f.write(msg)
print('msg: {}'.format(msg))

但是,我真正想要的是直接向子进程的标准输入发送消息而无需人工干预

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

尝试 2:通过管道然后写入父级标准输入。

父进程:hello_childstdin.js

const {join} = require('path');
const {spawn} = require('child_process');

const child = spawn('/usr/local/bin/python3', [join(__dirname, 'hello_childstdin.py')]);

process.stdin.pipe(child.stdin);

// Trying to write to parent process stdin
process.stdin.write('hello\n');

child.stdout.on('data', (data) => {
console.log(`child stdout: \n${data}`);
});

child.stderr.on('data', (data) => {
console.log(`child stderr: \n${data}`);
});

尝试 3:写入子标准输入。

父进程:hello_childstdin.js

const {join} = require('path');
const {spawn} = require('child_process');

const child = spawn('/usr/local/bin/python3', [join(__dirname, 'hello_childstdin.py')]);

process.stdin.pipe(child.stdin);

// Trying to write to parent process stdin
child.stdin.write('hello\n');

child.stdout.on('data', (data) => {
console.log(`child stdout: \n${data}`);
});

child.stderr.on('data', (data) => {
console.log(`child stderr: \n${data}`);
});

尝试 4

查看 child.stdin 的文档解释:

If the child is waiting to read all its input, it will not continue until this stream has been closed via end().

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

// ... same as above ...

child.stdin.write('hello\n');
child.stdin.end();

// ... same as above ...

这会结束子进程,并且也不会覆盖消息。

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

最佳答案

想通了:我可以将可读流推送给 child 。

...

const stream = require('stream');

...

var stdinStream = new stream.Readable();
stdinStream.push('hello\n'); // Add data to the internal queue for users of the stream to consume
stdinStream.pipe(child.stdin);

感谢this answer .

关于javascript - NodeJS : Send message to persistent child process's stdin without human interaction?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60450411/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com