Python等待数据在sys.stdin中 [英] Python wait until data is in sys.stdin

查看:45
本文介绍了Python等待数据在sys.stdin中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题如下:

我的pythons脚本通过sys.stdin接收数据,但需要等到sys.stdin上有新数据可用.

My pythons script receives data via sys.stdin, but it needs to wait until new data is available on sys.stdin.

如 python 的联机帮助页所述,我使用以下代码,但它完全使我的 CPU 过载.

As described in the manpage from python, i use the following code but it totally overloads my cpu.

#!/usr/bin/python -u
import sys
while 1:
     for line in sys.stdin.readlines():
         do something useful

有什么好的办法可以解决cpu占用高的问题?

Is there any good way to solve the high cpu usage?

您的所有解决方案都不起作用.我告诉你我的问题.

All your solutions don't work. I give you exactly my problem.

您可以将 apache2 守护进程配置为将每个日志行发送到程序而不是写入日志文件.

You can configure the apache2 daemon that he sends every logline to a program and not to write in a logfile.

看起来像这样:

CustomLog "|/usr/bin/python -u /usr/local/bin/client.py" combined

Apache2 期望我的脚本始终运行,等待 sys.stdin 上的数据并解析它然后有数据.

Apache2 expects from my script that it runs always, waits for data on sys.stdin and parses it then there is data.

如果我只使用 for 循环,脚本将退出,因为 sys.stdin 中没有数据,apache2 会说 ohh 你的脚本意外退出.

If i only use a for loop the script will exit, because at a point there is no data in sys.stdin and apache2 will say ohh your script exited unexpectedly.

如果我使用 while true 循环,我的脚本将使用 100% 的 CPU 使用率.

If i use a while true loop my script will use 100% cpu usage.

推荐答案

以下应该可以正常工作.

The following should just work.

import sys
for line in sys.stdin:
    # whatever

理由:

代码将在 stdin 进入时迭代它们.如果流仍然打开,但没有完整的行,则循环将挂起,直到遇到换行符(并返回整行)或者流被关闭(并且返回缓冲区中剩余的任何内容).

The code will iterate over lines in stdin as they come in. If the stream is still open, but there isn't a complete line then the loop will hang until either a newline character is encountered (and the whole line returned) or the stream is closed (and the whatever is left in the buffer is returned).

一旦流关闭,就不能再向标准输入写入或读取数据.期间.

Once the stream has been closed, no more data can be written to or read from stdin. Period.

您的代码使您的 cpu 过载的原因是,一旦 stdin 关闭,任何后续对 stdin 进行迭代的尝试都将立即返回而不执行任何操作.本质上,您的代码等效于以下内容.

The reason that your code was overloading your cpu is that once the stdin has been closed any subsequent attempts to iterate over stdin will return immediately without doing anything. In essence your code was equivalent to the following.

for line in sys.stdin:
    # do something

while 1:
    pass # infinite loop, very CPU intensive

如果您发布如何将数据写入标准输入,也许会很有用.

Maybe it would be useful if you posted how you were writing data to stdin.

Python 将(出于 for 循环、迭代器和 readlines() 的目的)在遇到 EOF 字符时认为流已关闭.您可以在此之后要求 Python 读取更多数据,但不能使用前面的任何方法.python手册页推荐使用

Python will (for the purposes of for loops, iterators and readlines() consider a stream closed when it encounters an EOF character. You can ask python to read more data after this, but you cannot use any of the previous methods. The python man page recommends using

import sys
while True:
    line = sys.stdin.readline()
    # do something with line

当遇到 EOF 字符时,readline 将返回一个空字符串.如果流仍处于打开状态,则对 readline 的下一次调用将正常运行.您可以通过在终端中运行命令来自行测试.按 ctrl+D 将导致终端将 EOF 字符写入标准输入.这将导致本文中的第一个程序终止,但最后一个程序将继续读取数据,直到实际关闭流.最后一个程序不应该 100% 你的 CPU,因为 readline 会等到有数据返回而不是返回一个空字符串.

When an EOF character is encountered readline will return an empty string. The next call to readline will function as normal if the stream is still open. You can test this out yourself by running the command in a terminal. Pressing ctrl+D will cause a terminal to write the EOF character to stdin. This will cause the first program in this post to terminate, but the last program will continue to read data until the stream is actually closed. The last program should not 100% your CPU as readline will wait until there is data to return rather than returning an empty string.

当我从实际文件尝试 readline 时,我只遇到忙循环的问题.但是当从 stdin 读取时,readline 愉快地阻塞了.

I only have the problem of a busy loop when I try readline from an actual file. But when reading from stdin, readline happily blocks.

这篇关于Python等待数据在sys.stdin中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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