node js 与 shell 应用程序交互 [英] node js interact with shell application
本文介绍了node js 与 shell 应用程序交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
网上有很多关于如何生成子进程然后将结果作为字符串捕获以供您自己处理的节点 js 示例.
There are plenty of node js examples on online about how to spawn a child process and then catch the result as a string for your own processing.
但是……
我想与子进程交互".例如,我将如何编写一个 node js 应用程序,而不是通过调用 'python
' 开始,然后键入一个语句 '1+1
',让我捕获结果 '2
',在继续输入另一个任意语句 '4+4
' 之前?
I want to 'interact' with a child process. For example, how would I write a node js application than starts by calling 'python
' and then types a statement '1+1
', lets me catch the result '2
', before proceeding to type another arbitrary statement '4+4
'?
(通过类型",我假设它需要将数据流式传输到进程使用的标准输入.
(And by 'type' I'm assuming it will require streaming data to the stdin that the process uses).
推荐答案
var child = require('child_process');
var ps = child.spawn('python', ['-i']);
ps.stdout.pipe(process.stdout);
ps.stdin.write('1+1');
ps.stdin.end();
效果很好!
这篇关于node js 与 shell 应用程序交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文