Python中的子流程实现,尝试从C++;程序 我有一个C++程序,我想连续运行,收集树莓PI上的传感器数据。我试图使用Python的子进程特性来启动C++程序,然后偶尔读取C++程序的输出。但是,我在读C++程序到Python数据方面遇到了困难。Python程序总是不返回任何内容。我做错了什么?是否有更可靠的方法从连续运行的C++程序中读取数据到Python程序? 这是C++程序: #include <stdio.h> #include <signal.h> #include <pigpio.h> #include <unistd.h> #include <stdlib.h> #include <signal.h> #include <math.h> #include <iostream> int main(int argc, char *argv[]) { std::cout<< "Test of disturbance input\n"; // Calculations are performed once every 0.25 seconds. time_sleep(0.25); }

Python中的子流程实现,尝试从C++;程序 我有一个C++程序,我想连续运行,收集树莓PI上的传感器数据。我试图使用Python的子进程特性来启动C++程序,然后偶尔读取C++程序的输出。但是,我在读C++程序到Python数据方面遇到了困难。Python程序总是不返回任何内容。我做错了什么?是否有更可靠的方法从连续运行的C++程序中读取数据到Python程序? 这是C++程序: #include <stdio.h> #include <signal.h> #include <pigpio.h> #include <unistd.h> #include <stdlib.h> #include <signal.h> #include <math.h> #include <iostream> int main(int argc, char *argv[]) { std::cout<< "Test of disturbance input\n"; // Calculations are performed once every 0.25 seconds. time_sleep(0.25); },python,c++,raspberry-pi3,Python,C++,Raspberry Pi3,除了我上面的评论之外,以下两种方法似乎都有效: while True: completed = subprocess.run(['./testSubprocess'], stdout=subprocess.PIPE) print(completed.stdout.decode()) 及 这可能是一个重复的问题。请参见:这进一步导致:您的C++程序打印一件事,然后退出。Python代码启动该程序一次,然后反复尝试从中读取。这些东西不匹配。 while True: comp

除了我上面的评论之外,以下两种方法似乎都有效:

while True:
    completed = subprocess.run(['./testSubprocess'], stdout=subprocess.PIPE)
    print(completed.stdout.decode())


这可能是一个重复的问题。请参见:这进一步导致:您的C++程序打印一件事,然后退出。Python代码启动该程序一次,然后反复尝试从中读取。这些东西不匹配。
while True:
    completed = subprocess.run(['./testSubprocess'], stdout=subprocess.PIPE)
    print(completed.stdout.decode())
while True:
    with subprocess.Popen(["./testSubprocess"], stdout=subprocess.PIPE, bufsize=1,
            universal_newlines=True) as p:
        for line in p.stdout:
            print(line)