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

2024/10/1 12:27:15

I encountered the picotui library, and was curious to know a bit how it works.

I saw here (line 147) that it uses:

os.read(0,32)

According to Google 0 represents stdin, but also that the accepted answer for reading from stdin is via

sys.stdin.buffer.read()

I was wondering what the difference is between the two. Which is faster? Which is the more portable version?

Answer

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). 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). 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).

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.

https://en.xdnf.cn/q/70964.html

Related Q&A

python - Pandas: groupby ffill for multiple columns

I have the following DataFrame with some missing values. I want to use ffill() to fill missing values in both var1 and var2 grouped by date and building. I can do that for one variable at a time, but w…

Gtk-Message: Failed to load module canberra-gtk-module

My pygtk program writes this warning to stderr:Gtk-Message: Failed to load module "canberra-gtk-module"libcanberra seems to be a library for sound.My program does not use any sound. Is there …

Why does installation of some Python packages require Visual Studio?

Say, you are installing a Python package for pyEnchant or crfsuite, etc. It fails to install and in the error trace it says some .bat (or .dll) file is missing.A few forums suggest you install Visual S…

Does Django ORM have an equivalent to SQLAlchemys Hybrid Attribute?

In SQLAlchemy, a hybrid attribute is either a property or method applied to an ORM-mapped class,class Interval(Base):__tablename__ = intervalid = Column(Integer, primary_key=True)start = Column(Integer…

Building a Python shared object binding with cmake, which depends upon external libraries

We have a c file called dbookpy.c, which will provide a Python binding some C functions.Next we decided to build a proper .so with cmake, but it seems we are doing something wrong with regards to linki…

What linux distro is better suited for Python web development?

Which linux distro is better suited for Python web development?Background:I currently develop on Windows and its fine, but I am looking to move my core Python development to Linux. Im sure most any di…

Relation between 2D KDE bandwidth in sklearn vs bandwidth in scipy

Im attempting to compare the performance of sklearn.neighbors.KernelDensity versus scipy.stats.gaussian_kde for a two dimensional array.From this article I see that the bandwidths (bw) are treated diff…

How to style (rich text) in QListWidgetItem and QCombobox items? (PyQt/PySide)

I have found similar questions being asked, but without answers or where the answer is an alternative solution.I need to create a breadcrumb trail in both QComboBoxes and QListWidgets (in PySide), and …

Reconnecting to device with pySerial

I am currently having a problem with the pySerial module in Python. My problem relates to connecting and disconnecting to a device. I can successfully connect to my device and communicate with it for a…

How to check if a sentence is a question with spacy?

I am using spacy library to build a chat bot. How do I check if a document is a question with a certain confidence? I know how to do relevance, but not sure how to filter statements from questions.I a…