read
method of files objects read until EOF. We know that EOF is not a value, but it's a condition which takes place when there's no more data to read from streams.
Wikipedia: EOF
In computing, end-of-file (commonly abbreviated EOF[1]) is a condition in a computer operating system where no more data can be read from a data source. The data source is usually called a file or stream.
This is all great. Contuning reading the same page:
Block-reading functions return the number of bytes read, and if this is fewer than asked for, then the end of file was reached or an error occurred (checking of
errno
or dedicated function, such asferror
is often required to determine which).
I could imagine that read
will request N number of bytes each time it read if N < reguest number of bytes then EOF. But If I had a command like this:
$ python writer.py | python reader.py
#writer.py
print("Hi", flush=True)
print("Billie Jean!")
import time
time.sleep(10)
print("Bye Billie :)")
#reader.py
import sys
print(sys.stdin.read())
print("after")
#print(input())
there are two scripts communicating via a pipeline. Basically, the first script's output stream is a pipe connected to the input stream of the second script, reader.py. However, there's something interesting, read
now should get puzzled! Since read
reads N number of bytes just like was mentioned above, well at least in my assumption read
reads N number of bytes I could be worng!
Then if read
retrieves N
number of bytes, the first print
in the script writer.py sends just the string "Hi"
to the pipe but the second print
doesn't get transmitted to the pipe and sits in buffer because I didn't flush the buffer, therefore next time read
requests N number of bytes from the pipe it should think EOF condition is satisfied? I know one thing, read
blocks until the first process finishes, unlike readline
for example which doesn't block. It seems read
is smarter than I thought, but how does it do its magic? How does it know that there's still a chunk of data to be read if it reads N number of bytes from pipe?
Billie Jean sits in buffer for 10s each time I run my scripts?! :P