0

I have an application(.exe) written in any language eg. C++ and want to run the application from python. I can run that simple application by using below sample Python code by following tutorial here 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++ code:

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

This works fine for me. But what if i am reading input in C++ application multiple times as below:

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

Here i am not able to use process.communicate() multiple times since the child has already exited by the time it returns.Basically i want to interact with the programme as a continuous session. I want suggestion or any other approach to solve this issue in python? Thanks in advance.

flamelite
  • 2,654
  • 3
  • 22
  • 42

1 Answers1

0

Assuming you are working on windows I would suggest having a look at namedpipes, on python end you can use PyWPipe, on c++ end you will have to write your own wrappers to get the message for the process.

amritanshu
  • 777
  • 13
  • 25