gpt4 book ai didi

python - 扭曲,ProcessProtocol 如何接收没有缓冲的标准输出?

转载 作者:行者123 更新时间:2023-11-28 17:50:56 26 4
gpt4 key购买 nike

我正在使用外部进程,它为处理的每个数据 block 写入短行输出。我想在每一行之后使用react,不要有任何额外的延迟。但是,似乎 ProcessProtocol.outReceived() 被缓冲了。文档状态:

.outReceived(data): This is called with data that was received from the process' stdout pipe. Pipes tend to provide data in larger chunks than sockets (one kilobyte is a common buffer size), so you may not experience the "random dribs and drabs" behavior typical of network sockets, but regardless you should be prepared to deal if you don't get all your data in a single call. To do it properly, outReceived ought to simply accumulate the data and put off doing anything with it until the process has finished.

结果是,在整个处理完成后,我得到了一个 block 的输出。如何强制 ProcessProtocol 不缓冲标准输出?

最佳答案

I'm using external process which writes short line of output for each chunk of data processed. I would like to react after each of these lines without any additional delay.

The result is, that I get output in one chunk after whole processing is done. How can I force ProcessProtocol not to buffer stdout?

缓冲发生在生产者进程中,而不是消费者。标准 C 库 stdout 仅在连接到终端时是行缓冲的,否则是全缓冲的。这就是导致生产者进程在未连接到终端时以大块而不是逐行输出数据的原因。

使用stdbuf强制生产者进程的 stdout 进行行缓冲的实用程序。

如果生产者进程是 python 脚本,请使用 -u python 解释器开关来完全关闭标准流的缓冲。 stdbuf 实用程序更好。

关于python - 扭曲,ProcessProtocol 如何接收没有缓冲的标准输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9923649/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com