只要一些可读取子进程的输出? [英] Reading child process' output as soon as some is available?

查看:108
本文介绍了只要一些可读取子进程的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试各种方法(的popen,管+叉/ EXEC,...)来读取子进程的输出,所有这些工作,但出现相同的行为:每当我试图读取输出使用 / FREAD ,只当缓冲区完全填满,或者孩子退出时返回。我正在寻找一个行为更像是插口。一旦读取任何数量的数据如一些可用

I've been trying various methods (popen, pipes + fork/exec, ...) to read a child process' output, all of which are working, but exhibit the same behavior: whenever I try to read the output using read/fread, it only returns when the buffer is completely full, or when the child exits. I'm looking for a behavior that's more like that of sockets: reading any amount of data as soon as some is available.

我该怎么办呢?

推荐答案

一般来说,你不知道。具体地,子进程将缓冲流,因为它不会看到连接到管道作为一个流互动。由于缓冲子进程内部发生,即将prevent的唯一方法是重写code在孩子prevent从缓冲它的标准输出(无论是曾经,或者通过一个特定的开关,或者你可以添加code,当它连接到一个管道检测和只有在特定情况关闭缓冲)。然而,这是否可以写入多标准输出(特别是如果你不挑剔,当您禁用缓冲)影响孩子的表现。

Generally you don't. In particular, the child process will buffer the stream because it won't see a stream connected to a pipe as being "interactive." Since the buffering is happening inside the child process, about the only way to prevent it is to rewrite the code in the child to prevent it from buffering its standard output (either ever, or when passed a particular switch, or perhaps you can add code to detect when it's connected to a pipe and turn off buffering only in that specific case). That, however, can affect the child's performance if it writes much to standard output (especially if you aren't selective about when you disable buffering).

这篇关于只要一些可读取子进程的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆