2017-07-14 6 views
0

私はどの言語で書かれたアプリケーション(.exe)も持っています。 C + +とPythonからアプリケーションを実行したい。Pythonの子プロセスにある別のアプリケーションとの対話セッション

#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; 
} 

これが私のために正常に動作します:私はここで次のチュートリアルでサンプルPythonコードの下に使用してhttps://docs.python.org/2/library/subprocess.html

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++のコードを単純なアプリケーションを実行することができます。しかし、私は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

関連する問題