python和c ++之间的进程间通信

Joh*_*Jos 5 c++ python interprocess

我有一个python脚本来处理数据(作为更大框架的一部分),还有一个c ++脚本来从仪器中收集数据。C ++代码不断收集,然后通过cout发出数据。

这些程序需要互相交谈,例如:

  • Python->调用开始收集的c ++脚本
  • (等待x时间)
  • Python->向C ++脚本发送命令以停止收集和发射数据
  • c ++->接收命令并对其执行操作
  • Python->接收数据

我在最后一步中挣扎,我觉得打断是最好的方法,但我可能错了吗?这是我目前所拥有的:

(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..
Run Code Online (Sandbox Code Playgroud)

(C ++主)

...
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();
...
Run Code Online (Sandbox Code Playgroud)

(C ++中断)

void interrupt(BOOL *read_flag)
{
    // Listen for Python command to interrupt reading
    *read_flag = FALSE;
}
Run Code Online (Sandbox Code Playgroud)

如果有人能指出我的观点,那将非常有帮助,很高兴采用任何可行的解决方案。

谢谢!!