Home > Article > Backend Development > Detailed explanation and examples of using asyncio to encapsulate file reading and writing in Python
">
Preface
Like network IO, file reading and writing is also a troublesome operation. .
By default, Python uses the system's blocking read and write. This means that if
f = file('xx') f.read()
is called in asyncio, the event loop will be blocked. .
This article briefly describes how to use asyncio.Future objects to encapsulate asynchronous reading and writing of files.
Currently only supports Linux.
##Blocking and non-blockingFirst, you need to change the reading and writing of the file to a non-blocking form. In the non-blocking case, each call to read will return immediately. If the return value is empty, then It means that the file operation has not been completed, otherwise it is the read file content. The switching between blocking and non-blocking is related to the operating system, so this article only writes the Linux version if you have ever done Unix system programming. Experience, you will find that the operation of Python is similar.flag = fcntl.fcntl(self.fd, fcntl.F_GETFL) if fcntl.fcntl(self.fd, fcntl.F_SETFL, flag | os.O_NONBLOCK) != 0: raise OSError()Future objectFuture object is similar to the Promise object in
Javascript It is a placeholder, its value. Will be calculated in the future. We can use
result = await future to return after the future gets the value and use future.set_result(xxx).
The value of the future can be set, which means that the future can be returned. The awaitoperatorwill automatically call future.result() to get the value. ##A
functioncan be inserted into the event loop through the loop.call_soon method.
At this point, our idea of asynchronous file reading and writing has come out. Call the function for non-blocking reading and writing of files. If the file reading and writing is not completed, calculate the number of bytes remaining to be read and written, and insert the event loop again until the reading and writing is completed. You can find that it is. In traditional Unix programming, the
whileloop for non-blocking file reading and writing is replaced by the asyncio event loop
The following is a schematic code for this process.
The above is the detailed content of Detailed explanation and examples of using asyncio to encapsulate file reading and writing in Python. For more information, please follow other related articles on the PHP Chinese website!