Python读取流 [英] Python read stream
问题描述
我需要一种非常的廉价方式来读取Python中没有终止字符串(流)的缓冲区.这就是我所拥有的,但是浪费了很多的CPU时间和精力.因为它是不断的尝试和捕捉".我真的需要一种新方法.
I need a very inexpensive way of reading a buffer with no terminating string (a stream) in Python. This is what I have, but it wastes a a lot of CPU time and effort. Because it is constantly "trying and catching." I really need a new approach.
这是我的代码的简化版本:
Here is a reduced working version of my code:
#! /usr/bin/env/ python
import fcntl, os, sys
if __name__ == "__main__":
f = open("/dev/urandom", "r")
fd = f.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
ready = False
line = ""
while True:
try:
char = f.read()
if char == '\r':
continue
elif char = '\n':
ready = True
else:
line += char
except:
continue
if ready:
print line
请勿在终端中运行此命令.这只是为了说明. "urandom"将破坏您的终端,因为它吐出了很多随机字符,终端仿真器会解释这些字符,而不管它是什么(这可以更改您当前的shell设置,标题等).我正在阅读通过USB连接的GPS.
Don't run this in the terminal. It's simply for illustration. "urandom" will break your terminal because it spits out a lot of random characters that the terminal emulator interprets no matter what (which can change your current shells settings, title, etc). I was reading from a gps connected via usb.
问题:这可能会占用100%的CPU使用率.我已经尝试过了:
The problem: this uses 100% of the CPU usage when it can. I have tried this:
#! /usr/bin/env/ python
import fcntl, os, sys
if __name__ == "__main__":
f = open("/dev/urandom", "r")
fd = f.fileno()
fl = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, fl | os.O_NONBLOCK)
for line in f.readlines():
print line
但是,我得到了IOError: [Errno 11] Resource temporarily unavailable
.除其他外,我尝试使用Popen
.我很茫然.有人可以提供解决方案吗(请说明一切,因为我本身不是专业人士).另外,我应该注意,这是针对Unix(尤其是Linux,但必须可在所有版本的Linux上移植).
However, I get IOError: [Errno 11] Resource temporarily unavailable
. I have tried to use Popen
amongst other things. I am at a loss. Can someone please provide a solution (and please explain everything, as I am not a pro, per se). Also, I should note that this is for Unix (particularly Linux, but it must be portable across all versions of Linux).
推荐答案
打开文件流时,您需要将缓冲模式设置为要读取的块的大小. 来自python文档:
You will want to set your buffering mode to the size of the chunk you want to read when opening the file stream. From python documentation:
io.open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True)
" buffering 是用于设置缓冲策略的可选整数.传递0以关闭缓冲(仅在二进制模式下允许),传递1以选择行缓冲(仅在文本模式下可用),和大于1的整数表示固定大小的块缓冲区的大小."
"buffering is an optional integer used to set the buffering policy. Pass 0 to switch buffering off (only allowed in binary mode), 1 to select line buffering (only usable in text mode), and an integer > 1 to indicate the size of a fixed-size chunk buffer."
您还希望在while循环中使用visible()方法,以避免不必要的资源消耗.
You also want to use the readable() method in the while loop, to avoid unnecessary resource consumption.
但是,我建议您使用缓冲流,例如io.BytesIO
或io.BufferedReader
However, I advise you to use buffered streams such as io.BytesIO
or io.BufferedReader
文档中的更多信息.
这篇关于Python读取流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!