简体   繁体   English

python中的os.read(0,)vs sys.stdin.buffer.read()

[英]os.read(0,) vs sys.stdin.buffer.read() in python

I encountered the picotui library, and was curious to know a bit how it works. 我遇到了picotui库,并很好奇它的工作原理。

I saw here (line 147) that it uses: 我在这里 (第147行)看到了它的用法:

os.read(0,32)

google says that 0 represents stdin, but also that the accepted answer for reading from stdin is via Google说0代表标准输入,但从标准输入中读取的可接受答案是通过

sys.stdin.buffer.read()

I was wondering what the difference is between the two. 我想知道两者之间有什么区别。 Which is faster? 哪个更快? Which is the more portable version? 哪个是更便携的版本?

Using os.read(0, 32) is an unbuffered read, directly invoking an OS-level syscall and reading only a very specific amount of data (with a firm guarantee that the individual call won't read more than that). 使用os.read(0, 32)无缓冲读取,直接调用OS级别的syscall并仅读取非常特定数量的数据(并保证单个调用读取的数据不会超过此数量)。 Sometimes -- for example, if you're going to be handing off your stdin to a different program and letting it read the rest of the pending data -- you specifically need that. 有时候-比如,如果你要你的标准输入被移交给不同的程序, 让它读取挂起数据的其余部分-特别需要这一点。

sys.stdin.buffer.read() is a buffered read (and, without specifying a length, one that reads as much as possible). sys.stdin.buffer.read()是一个缓冲的读取(并且在不指定长度的情况下,读取的长度尽可能多)。 A buffered read can potentially read more data that you're immediately asking for (even though it only returns the requested amount, keeping the rest in a buffer to use to handle future requests), to reduce the number of syscalls made and thus operate with lower context-switch overhead when reading lots of data (in particular, when you would otherwise be doing lots of short reads, buffering reduces the number of round-trips between userland and the OS kernel). 缓冲读取可以潜在地读取您立即要求的更多数据(即使它仅返回请求的量,将其余的保留在缓冲区中以用于处理将来的请求),以减少进行的系统调用数量,从而可以减少读取大量数据时的上下文切换开销(尤其是当您原本会进行大量短读时,缓冲会减少用户层和OS内核之间的往返次数)。

Which of these is appropriate is deeply dependent on implementation and runtime-envionment details, and a question asking which is appropriate for a given scenario would need to include more details to not be overbroad. 其中哪一个合适,在很大程度上取决于实现和运行时环境的详细信息,而一个询问哪个问题适合于给定场景的问题将需要包括更多的细节,以免泛滥。 What's important is that you don't mix them ; 重要的是不要混在一起 performing an unbuffered read after a buffered read can have unpredictable results, since there's no way of knowing how much data was already read by the OS to populate as-yet-unused parts of the read buffer. 在缓冲读取之后执行无缓冲读取可能会产生不可预测的结果,因为无法知道操作系统已经读取了多少数据来填充读取缓冲区的尚未使用的部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM