非阻塞文件读取 [英] Non-blocking file read
问题描述
如何在非阻塞模式下读取二进制文件或文本文件的内容?
How can I read the contents of a binary or a text file in a non-blocking mode?
对于二进制文件:当我open(filename, mode='rb')
时,我得到一个io.BufferedReader
的实例. io.BufferedReader.read
说的文档:
For binary files: when I open(filename, mode='rb')
, I get an instance of io.BufferedReader
. The documentation fort io.BufferedReader.read
says:
读取并返回大小字节,或者如果未指定大小或为负数,则直到EOF或读取调用将在非阻塞模式下阻塞.
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.
很明显,简单的open(filename, 'rb').read()
处于阻止模式.令我惊讶的是,我在io
文档的任何地方都找不到关于如何选择非阻塞模式的解释.
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.
对于文本文件:当我open(filename, mode='rt')
时,我得到io.TextIOWrapper
.我假设相关文档是read
在其基类io.TextIOBase
中的文档;和根据这些文档,似乎没有办法完全不阻塞读取:
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:
作为单个str从流中读取并返回最大大小的字符.如果size为负数或无,则读取直到EOF.
Read and return at most size characters from the stream as a single str. If size is negative or None, reads until EOF.
推荐答案
文件操作被阻止.没有非阻塞模式.
File operations are blocking. There is no non-blocking mode.
但是您可以创建一个在后台读取文件的线程.在Python 3中, concurrent.futures
模块可以在这里有用.
But you can create a thread which reads the file in the background. In Python 3, concurrent.futures
module can be useful here.
from concurrent.futures import ThreadPoolExecutor
def read_file(filename):
with open(filename, 'rb') as f:
return f.read()
executor = concurrent.futures.ThreadPoolExecutor(1)
future_file = executor.submit(read_file, 'C:\\Temp\\mocky.py')
# continue with other work
# later:
if future_file.done():
file_contents = future_file.result()
或者,如果您需要在操作完成后调用回调:
Or, if you need a callback to be called when the operation is done:
def on_file_reading_finished(future_file):
print(future_file.result())
future_file = executor.submit(read_file, 'C:\\Temp\\mocky.py')
future_file.add_done_callback(on_file_reading_finished)
# continue with other code while the file is loading...
这篇关于非阻塞文件读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!