Python uses select to Implement Asynchronous IO

Source: Internet
Author: User

Python uses select to Implement Asynchronous IO

This example describes how to Implement Asynchronous IO Through select in Python. Share it with you for your reference. The details are as follows:

Using select in Python is much easier than using poll in C. The parameters of the select function are three lists that contain integer file descriptors or fileno () method objects with the returned file descriptors. The first parameter is the object waiting for input, the second parameter specifies the object waiting for output, and the third parameter specifies the object in abnormal conditions. The fourth parameter is the set timeout time, which is a floating point number. Specify the timeout value in seconds. The select function returns a set of file descriptors, including input, output, and exceptions.

In linux, the select statement is used to implement multi-path IO File Replication programs:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

#! /Usr/bin/env python

Import select

# Import the select module

BLKSIZE = 8192

Def readwrite (fromfd, tofd ):

Readbuf = fromfd. read (BLKSIZE)

If readbuf:

Tofd. write (readbuf)

Tofd. flush ()

Return len (readbuf)

Def copy2file (fromfd1, tofd1, fromfd2, tofd2 ):

'''Using select to choice fds '''

Totalbytes = 0

If not (fromfd1 or fromfd2 or tofd1 or tofd2 ):

# Check whether all file descriptors are valid

Return 0

While True:

# Start to use select to monitor all input file descriptors

Rs, ws, es = select. select ([fromfd1, fromfd2], [], [])

For r in rs:

If r is fromfd1:

# Read data when the first file descriptor is readable

Bytesread = readwrite (fromfd1, tofd1)

Totalbytes + = bytesread

If r is fromfd2:

Bytesread = readwrite (fromfd2, tofd2)

Totalbytes + = bytesread

If (bytesread <= 0 ):

Break

Return totalbytes

Def main ():

Fromfd1 = open ("/etc/fstab", "r ")

Fromfd2 = open ("/etc/passwd", "r ")

Tofd1 = open ("/root/fstab", "w + ")

Tofd2 = open ("/root/passwd", "w + ")

Totalbytes = copy2file (fromfd1, tofd1, fromfd2, tofd2)

Print "Number of bytes copied % d \ n" % totalbytes

Return 0

If _ name __= = "_ main __":

Main ()

I hope this article will help you with Python programming.

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.