【问题标题】:Running a c++ and a python script simultaneously causing problems同时运行 c++ 和 python 脚本会导致问题
【发布时间】:2015-10-12 15:24:56
【问题描述】:

目前我遇到了问题,我不得不将我的项目拆分为 python 和 c++ 脚本,因为它们具有我需要的硬件指定库。作为我项目的一部分,c++ 脚本为我的 LED 灯条创建 rgb 数据,python 脚本将它们上传到 LED 灯。因此,我需要在程序之间进行信息交换,因为 LED 必须一直刷新,这将我们引向主要问题。 c++ 脚本将数据写入 .txt 文件并写入 trigger2.txt 文件。 python 脚本一直在等待触发文件,直到它出现才开始。 Python 脚本完成后,它还会创建一个 trigger.txt 文件,以便 c++ 脚本知道它可以重新启动。 当我运行我的程序时,它会上传一次 rgb 数据,然后停止。似乎在c ++脚本启动python脚本并上传了rgb数据后,c ++无法继续,因为python脚本在while循环中要求trigger2.txt。就像 c++ 脚本作为进程列表中的第二个等待完成 python 脚本一样。

这是 c++ 脚本:

int main(){
int counter = 0;
int a;

while(1){

a=0;
  if (counter>0){                   //If counter==0 it's the first time an no trigger is needed
    while(a<1){
        std::ifstream FileTest("trigger.txt");  //If the trigger file exists the script can start again
        if(FileTest){
            a++;
            system("sudo rm trigger.txt");      //cleaning the folder
            }
        else
            std::cout << "Can't find trigger.txt" << std::endl;
    }
  }
if (a==1||counter==0){

/////The rgb data is generated here

    std::fstream file;              //Writing the .txt to transfer the rgb data
    file.open("rgb.txt", std::ios::out);
    for (int i = 0; i < 3;i++)
            file << rgbmatrix[i]<< std::endl;
    file.close();
    system("sudo touch trigger2.txt");      //The Python script can start now to refresh the led's

    if (counter==0){                //The first time led.py will be started manually
    system("sudo python led.py");
    zaehler++;
    }
}
}
    return 0;
}

这是python脚本:

rgb = [1,2,3]
i=0
import time
import sys
import os

while 1:
  if (os.path.exists('/home/pi/rgbwired_v2/trigger2.txt')):
    os.system("sudo rm trigger2.txt")
    file = open("rgb.txt","r")
    rgb = file.readlines()
    file.close()
    rgb = [int(i) for i in rgb]

    #RGB data will be uploaded here

    sys.stdout.write('led printed')
    os.system("sudo rm rgb.txt")
    os.system("sudo touch trigger.txt")

  else:
    sys.stdout.write('File not found')
    time.sleep(1)

感谢您的所有帮助。谢谢。 (我正在使用 Raspberry Pi)

【问题讨论】:

  • 从python脚本中调用c++程序会不会有问题?因为如果没有,你可以试试。
  • 感谢您的回答,它并没有解决问题,但现在我很确定我的怀疑是正确的。现在,在我更改了我的代码之后,它只会不断地运行我的 c++ 脚本的 while 循环。脚本似乎真的不能交替。我能做些什么呢:/?

标签: python c++ linux raspberry-pi


【解决方案1】:

为了让 C++ 程序完成它必须完成的工作,您可以从您的 python 脚本中调用它,读取它的输出并在您需要做更多工作时发送一些输入。为了实现这一点,您可以使用 python 提供的subprocess 库。

然后,您的 C++ 代码需要等待一些特殊输入(例如,换行符 '\n' 或类似的东西),然后计算需要计算的内容,然后将数据写入stdout,然后写一行表示数据集的结束(例如,简单地写'end',或一个空行)

你的 python 代码看起来像这样:

import subprocess

#Start the c++ program
process = subprocess.Popen(['<path to the c++ executable>', <arg1>, <arg2], stdin=subprocess.PIPE)
#Grab its output
p_out = process.stdout
#Grab its input
P_in = p.stdin

while True:
    p_in.write('\n')
    lines = []
    line = p_out.readline().decode("utf-8").strip()
    while line:
      <process the read data>
      line = p_out.readline().decode("utf-8").strip() 

【讨论】:

  • 我不熟悉这个子进程,但是当你说它等待c++程序终止时,你的意思是它完全完成了?由于 c++ 程序的初始化花费了太多时间,我无法一次又一次地启动它。完全终止是不可能的,这就是我使用 while 循环的原因。
  • 我编辑了我的答案,所以你只需要启动 c++ 程序一次。
猜你喜欢
  • 2020-09-05
  • 1970-01-01
  • 2021-08-28
  • 2011-05-11
  • 2021-12-26
  • 2014-09-03
  • 1970-01-01
  • 1970-01-01
  • 2013-03-06
相关资源
最近更新 更多