gpt4 book ai didi

node.js - Nodejs子进程: write to stdin from an already initialised process

转载 作者:IT老高 更新时间:2023-10-28 21:54:12 30 4
gpt4 key购买 nike

我正在尝试使用 Node 的 child_process 生成一个外部进程 phantomjs,然后在初始化后向该进程发送信息,这可能吗?

我有以下代码:

var spawn = require('child_process').spawn,
child = spawn('phantomjs');

child.stdin.setEncoding = 'utf-8';
child.stdout.pipe(process.stdout);

child.stdin.write("console.log('Hello from PhantomJS')");

但我在标准输出上得到的唯一信息是 phantomjs 控制台的初始提示。

phantomjs> 

所以看来 child.stdin.write 没有任何效果。

我不确定是否可以在初始生成时向 phantomjs 发送其他信息。

提前致谢。

最佳答案

您还需要传递 \n 符号才能使您的命令正常工作:

var spawn = require('child_process').spawn,
child = spawn('phantomjs');

child.stdin.setEncoding('utf-8');
child.stdout.pipe(process.stdout);

child.stdin.write("console.log('Hello from PhantomJS')\n");

child.stdin.end(); /// this call seems necessary, at least with plain node.js executable

关于node.js - Nodejs子进程: write to stdin from an already initialised process,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13230370/

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