Interprocess communication between python and C ++

I have a python script to process data (as part of a larger structure) and a C ++ script to collect data from the tool. C ++ code continuously collects, then emits data through cout.

Programs should talk to each other like this:

  • Python -> C ++ script calls that start to compile
  • (Waiting for time x time)
  • Python -> Sends a command to a C ++ script to stop collecting and emit data
  • C ++ -> Gets a command and acts on it
  • Python -> Gets data

I struggle with the last step, I feel that interruption is the best way to do this, but am I probably mistaken? Here is what I have at the moment:

(Python)

p = Popen("C++Script.exe", stdin=PIPE, stdout=PIPE, bufsize=1, shell=True)

# Wait for stuff to happen
# Send command to change read_flag to False in c++ script

for line in iter(p.stdout.readline, ''):
    line = str(line)  # Contains data to be parsed, obtained from cout
    # Do stuff..

.

(C ++ main)

...
BOOL read_flag = TRUE;
thread t1{ interrupt, &read_flag };

float Temp;
vector <float> OutData;

//Data read and stored in vector
for (int i = 1; read_flag == TRUE; i++) 
{
    Temp = Get_Single_Measurement();
    OutData.push_back(Temp); 
}

//Data read out in one go
for (int i = 0; i < OutData.size(); i++)
{
    cout << OutData[i] << endl;  //Picked up by python code
}

t1.join();
...

(interruption C ++)

void interrupt(BOOL *read_flag)
{
    // Listen for Python command to interrupt reading
    *read_flag = FALSE;
}

- , , , .

!!

+4

Source: https://habr.com/ru/post/1686392/


All Articles