好的,我试图从python脚本运行一个C程序.目前我正在使用测试C程序:
#include <stdio.h>
int main() {
while (1) {
printf("2000\n");
sleep(1);
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
模拟我将要使用的程序,它不断地从传感器获取读数.然后我试图"2000"从C程序读取输出(在这种情况下)与python中的子进程:
#!usr/bin/python
import subprocess
process = subprocess.Popen("./main", stdout=subprocess.PIPE)
while True:
for line in iter(process.stdout.readline, ''):
print line,
Run Code Online (Sandbox Code Playgroud)
但这不起作用.从使用print语句开始运行该.Popen行然后等待for line in iter(process.stdout.readline, ''):,直到我按下Ctrl-C.
为什么是这样?这正是我见过的大多数示例都是他们的代码,但它没有读取文件.
编辑:
有没有办法让它只在有东西被阅读时才能运行?
Platform: windows 8.1 IDE: vs2013 use c/c++
Process A read stdout of subprocess using pipe redirect.
but subprocess dont invoke fflush after printf, so processs A cant read anything from pipe before subprocess run to end.
ps: I have souce code of subprecess, but its very hard to modify them.
So whether Process A can force subprocess refresh stdout buffer to read something before subprocess run to end? (the same effective as fflush)
我有一个问题,我需要实时读取控制台输出。我有一个需要执行的文件,尝试执行类似的操作test.exe > text.txt,但是当我尝试在 exe 文件运行时读取时,我看不到任何内容,直到 exe 完成并同时写入所有行。我需要使用 node.js 来完成此操作