我正在阅读linux内部构件和syscall,我对术语IO Stream
很感兴趣(这很遗憾,因为我是一个经验丰富的程序员)。你知道吗
我对IO流真正含义的最好猜测是,它是将数据作为字节序列从一个文件抽象传输到另一个文件抽象的操作。你知道吗
很明显,这不是缓冲和流之间的区别问题。你知道吗
缓冲是一种实现。之所以使用它,是因为将字节从设备传输到内存的速度很慢,而且不需要每隔几个字节进行一次传输,因为:
我想做一些分析,所以我查看了python文档,在一个简单的open
上做了一个strace
:
io.open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True)
Open file and return a corresponding stream. If the file cannot be opened, an IOError is raised.
假设对于文本文件,这将是io.TextBase。你知道吗
strace
的结果是:
#!/usr/bin/env python3
with open('./sample.txt') as f:
print(f.read())
...
# returns 3 which is the first available file descriptor
openat(AT_FDCWD, "./sample.txt", O_RDONLY|O_CLOEXEC) = 3
# check it's not a directory and lseek to 0
lseek(3, 0, SEEK_CUR)
read(3, "streams of screams because I've "..., 46) = 45
read(3, "", 1) = 0
write(1, "streams of screams because I've "..., 46streams of screams because I've read too much
) = 46
close(3)
因此python解释器从当前工作目录执行openat
,因为我在文件路径上使用了/。然后一些检查和lseek
将标记移动到文件中的第一个字节。你知道吗
然后read
将文件write
发送到stdout。你知道吗
我想让我困惑的是python文档中的一行,它说返回一个相应的流。你知道吗
例如,TextBase
类是什么使它成为相应流的?你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐