2017-07-14 23 views
0

我有用任何语言编写的应用程序(.exe),例如。 C++并希望从python运行应用程序。我可以用下面的示例Python代码通过下面的教程在这里运行简单的应用https://docs.python.org/2/library/subprocess.html与Python中的子进程中的另一个应用程序的交互式会话

from subprocess import Popen, PIPE 

process = Popen([r"C:\Users\...\x64\Debug\Project13.exe"],stdout = PIPE, 
stderr=PIPE, stdin = PIPE) 
stdout = process.communicate(input = b"Bob")[0] 
print(stdout) 

C++代码:

#include <iostream> 
#include <windows.h> 
#include <string> 

using namespace std; 

void foo(string s) { 
    for (int i = 0; i < 3; ++i) 
    { 
     cout << "Welcome " << s << " "; 
     Sleep(2000); 
    } 
} 

int main() 
{ 
    string s; 
    cin>> s; 
    foo(s); 
    return 0; 
} 

这工作对我很好。但是,如果我在C++应用程序多次读取输入如下内容:

#include <iostream> 
#include <windows.h> 
#include <string> 

using namespace std; 

int main() 
{ 
    string s; 
    for (int i = 0; i < 3; ++i) 
    { 
     cin >> s; 
     cout << "Welcome " << s << " "; 
     Sleep(2000); 
    } 
    return 0; 
} 

我在这里无法使用process.communicate()多次既然孩子已经通过了时间退出它returns.Basically我想要作为连续会话与程序交互。 我想要建议或任何其他方法来解决这个问题在Python中?提前致谢。

回答

0

假设你正在使用windows,我建议看看namedpipes,在python结束你可以使用PyWPipe,在C++端,你将不得不编写你自己的包装来获取该过程的消息。

+0

看来这可能会让我通过并检查它是否有效。谢谢 – flamelite

相关问题