【发布时间】:2021-07-18 18:29:06
【问题描述】:
我正在尝试使用子进程将字符串从 C++ 程序连续发送到 Python 程序。
C++ 程序连续运行。
这是我的 C++ 程序:
#include <iostream>
#include <math.h>
#include <unistd.h>
int main(){
std::printf("This Sucks");
}
这是我的 Python 程序:
import os
import signal
from subprocess import Popen, PIPE, STDOUT
process = Popen('./Subprocess', stdin=PIPE,stdout=PIPE, universal_newlines = True, shell = True, preexec_fn = os.setsid)
while True:
output = process.stdout.readline()
if output == '' and process.poll is not None:
break
if output:
print(output)
在上面显示的表单中,Python 程序将读入并打印一次“This Sucks”。但是,如果我将 C++ 程序中的 print 语句放在一个循环中以使其重复打印,则 Python 程序会挂起并且什么也不打印。
我需要 C++ 程序中的 print 语句在 while 循环中连续运行,而 Python 程序必须能够从 C++ 程序中读取,因为它无限期地运行并且一遍又一遍地连续打印“This Sucks”。
最终目标是让 C++ 程序不断打印出传感器数据,而 Python 程序能够读取它。
我需要做些什么改变才能做到这一点?
【问题讨论】:
标签: python c++ python-3.x raspberry-pi