Read and write in Python using Asyncio package files and examples

Source: Internet
Author: User

This paper mainly gives you a brief explanation of how to use Asyncio. Future Object to encapsulate the asynchronous read and write of the file. A small partner in need can refer to the following

">

Objective

Like network IO, file read and write is also a cumbersome operation.

By default, Python uses blocking read and write for the system. This means that if you call the Asyncio in the

f = file (' xx ') F.read ()

The event loop is blocked.

This article briefly describes how to use Asyncio. Future object to encapsulate the asynchronous read and write of the file.

Code on GitHub. Currently only Linux is supported.

Blocking and non-blocking

First, you need to change the file read/write to a non-blocking form. In a non-blocking situation, every call to read will return immediately, and if the return value is null, it means that the file operation has not completed and vice versa is the contents of the file being read.

Blocking and non-blocking switching are related to the operating system, so this article only writes the Linux version temporarily. If you have experience with Unix system programming, you will find that Python operations are 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 objects

The future object resembles a Promise object in Javascript. It is a placeholder whose value is calculated in the future. We can use

result = await future

Returns after the value of the future is obtained. and using

Future.set_result (XXX)

You can set the value of the future, which means that the future can be returned. The await operator automatically calls Future.result () to get the value.

Loop.call_soon

A function can be inserted into the event loop by means of the Loop.call_soon method.

At this point, our asynchronous file reading and writing ideas are out. A function that calls non-blocking read and write files through Loop.call_soon. If the file reads and writes are not completed, the remaining number of bytes read and write is computed and the event loop is inserted again until read and write is completed.

It can be found that the traditional Unix programming, non-blocking files read and write the while loop into the Asyncio event loop.

The following is a schematic code for this process.

def read_step (self, future, N, total): res = Self.fd.read (n) if Res is None:sel    F.loop.call_soon (Self.read_step, future, N, total) return if not res: # EOF Future.set_result (bytes (self.rbuffer)) Return Self.rbuffer.extend (res) Self.loop.call_soon (Self.read_step, Future, self. Block_size, total) def read (self, n=-1): the future = Asyncio. The Future (Loop=self.loop) Self.rbuffer.clear () Self.loop.call_soon (Self.read_step, the Future, Min. Block_size, N), N) return to the future 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.