Detailed explanation and examples of using asyncio to encapsulate file reading and writing in Python

高洛峰
Release: 2017-03-28 15:24:31
Original
2593 people have browsed it

This article mainly explains how to use asyncio.Future object to encapsulate asynchronous reading and writing of files. Friends in need can refer to the following

">

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()
Copy after login

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-blocking

First, 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()
Copy after login
Future object

Future 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 await

operatorwill automatically call future.result() to get the value. ##A

function

can 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

while

loop 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template