相关疑难解决方法(0)

Python C程序子进程挂起"for line in iter"

好的,我试图从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.

为什么是这样?这正是我见过的大多数示例都是他们的代码,但它没有读取文件.

编辑:

有没有办法让它只在有东西被阅读时才能运行?

c python subprocess hang

17
推荐指数
2
解决办法
5446
查看次数

How fo force subprocess to refresh stdout buffer?

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)

subprocess fflush

6
推荐指数
0
解决办法
1324
查看次数

使用 node.js 从标准输出实时读取

我有一个问题,我需要实时读取控制台输出。我有一个需要执行的文件,尝试执行类似的操作test.exe > text.txt,但是当我尝试在 exe 文件运行时读取时,我看不到任何内容,直到 exe 完成并同时写入所有行。我需要使用 node.js 来完成此操作

console stdout file node.js

3
推荐指数
1
解决办法
1万
查看次数

标签 统计

subprocess ×2

c ×1

console ×1

fflush ×1

file ×1

hang ×1

node.js ×1

python ×1

stdout ×1