How can I read the contents of a binary or a text file in a non-blocking mode?
For binary files: when I open(filename, mode='rb')
, I get an instance of io.BufferedReader
. The documentation fort io.BufferedReader.read
says:
Read and return size bytes, or if size is not given or negative, until EOF or if the read call would block in non-blocking mode.
Obviously a straightforward open(filename, 'rb').read()
is in a blocking mode. To my surprise, I could not find an explanation anywhere in the io
docs of how to choose the non-blocking mode.
For text files: when I open(filename, mode='rt')
, I get io.TextIOWrapper
. I assume the relevant docs are those for read
in its base class, io.TextIOBase
; and according to those docs, there seems no way to do non-blocking read at all:
Read and return at most size characters from the stream as a single str. If size is negative or None, reads until EOF.