简体   繁体   English

Node.js中完全交互的子进程

[英]Fully interacting child process in Node.js

I have a simple C++ program compiled using command gcc 1.cpp -o 1.exe . 我有一个使用命令gcc 1.cpp -o 1.exe编译的简单C ++程序。

// 1.cpp    
#include <stdio.h>
int main(){
    int a = 0;
    scanf("%d", &a);
    printf("%d", a + 1000);
    scanf("%d", &a);
    printf("\n%d", a + 1000);
    return 0;
}

My goal is to create Node.js application which will be able to pass one number to 1.exe , then display result, and after that pass displayed result as next number. 我的目标是创建Node.js应用程序,该应用程序可以将一个数字传递给1.exe ,然后显示结果,然后将显示的结果作为下一个数字传递。 Finally, application must write second result and then close. 最后,应用程序必须写入第二个结果,然后关闭。
I've searched on Node.js manual's and documentations, and also on other sites, but I still cannot figure out how to create interactive child process in Node.js. 我在Node.js手册和文档以及其他站点上进行了搜索,但是仍然无法弄清楚如何在Node.js中创建交互式子流程。 The following code simply doesn't work. 以下代码根本不起作用。

var cp = require('child_process');
var proc = cp.spawn(pathToFile);
var n = 0;

proc.stdout.on('data', function(a){
    process.stdout.write(a);
    if(!n++) proc.stdin.write(a + '\n');
});

proc.stdin.write('1\n');

The function proc.stdout.on(data, ...) will never be reached, because it is called only when child process exit. 永远不会到达函数proc.stdout.on(data, ...) ,因为仅当子进程退出时才调用该函数。 Problem is because it will exit when second number is passed, but second number is passed when first result is received. 问题是因为当第二个数字通过时它将退出,但是在收到第一个结果时第二个通过了。 I've tried many combinations and many options in cp.spawn function, but nothing worked as I expected. 我已经尝试了cp.spawn函数中的许多组合和许多选项,但是没有按预期工作。
The reason I want fully interactive child process is because I want to create Node.js module which use external application which requires a long time to load, so easier way is to keep child process active and just to send and receive data through stdio. 我想要完全交互式的子进程的原因是因为我想创建Node.js模块,该模块使用需要长时间加载的外部应用程序,所以更简单的方法是保持子进程处于活动状态,并仅通过stdio发送和接收数据。
The C++ application is just an example. C ++应用程序只是一个例子。 The real application I want Node.js to interact with is something I cannot edit, so do not expect from me to change the code of that application. 我想与Node.js进行交互的真实应用程序是我无法编辑的,因此不要指望我更改该应用程序的代码。 I must adapt Node.js code to interact with application, not vice versa. 我必须修改Node.js代码才能与应用程序交互,反之亦然。 When I execute external application with command prompt, I receive output after every input line, so it is not problem with that application. 当我在命令提示符下执行外部应用程序时,我在每行输入后都会收到输出,因此该应用程序没有问题。
Any ideas how to make fully interacting child process? 有什么想法可以使孩子的过程充分互动吗?

Did you tried fflush? 你有尝试过脸红吗?

 printf("%d", a + 1000);
 fflush(stdout);

ok you said that u can't change C code. 好的,您说您无法更改C代码。 but in that case I suggest you write another C program (broker.c) between node & 1.cpp. 但在那种情况下,我建议您在node&1.cpp之间编写另一个C程序(broker.c)。 and invoke broker.cpp from node. 并从节点调用broker.cpp。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM