Python implements asynchronous IO through poll, and pythonpoll asynchronous io

Source: Internet
Author: User

Python implements asynchronous IO through poll, and pythonpoll asynchronous io

This example describes how Python implements asynchronous IO through poll. Share it with you for your reference. The specific analysis is as follows:

Poll object is returned after poll () is used. This object supports the following methods:
PollObj. register (fd, [, eventmask]) the first parameter is to register a new file descriptor fd. fd is either an integer file descriptor or a fileno () that obtains the file descriptor () method object. Eventmask is a bit or tag that indicates the event to be processed.

POLLIN: used to read data
POLLPRI: used to read Urgent Data
POLLOUT: Write preparation
POLLERR: Error
POLLHUP: Keep status
POLLNVAL: Invalid request

Finally, pollObj. poll () is used in the loop to round the registered file descriptor. Returns the First Ancestor (fd, event ). Fd is the file descriptor, and event is the bit mask indicating the time. You only need to test the event and the corresponding time.

Use poll to create a multi-path file replication program. The Code is as follows:

#! /Usr/bin/env pythonimport selectBLKSIZE = 8192def readwrite (fromfd, tofd): readbuf = fromfd. read (BLKSIZE) if readbuf: tofd. write (readbuf) tofd. flush () return len (readbuf) def copyPoll (fromfd1, tofd1, fromfd2, tofd2): # define the event READ_ONLY = (select. POLLIN | select. POLLPRI | select. POLLHUP | select. POLLERR) totalbytes = 0 if not (fromfd1 or fromfd2 or tofd1 or tofd2): return 0 fd_dict = {fromfd1.fileno (): fr Omfd1, fromfd2.fileno (): fromfd2} # create poll object p = select. poll () # Use the poll object p to register the file descriptor to be monitored. register (fromfd1, READ_ONLY) p. register (fromfd2, READ_ONLY) while True: # Check whether the registered file descriptor is ready. result = p. poll () if len (result )! = 0: for fd, events in result: if fd_dict [fd] is fromfd1: if events & (select. POLLIN | select. POLLPRI): bytesread = readwrite (fromfd1, tofd1) totalbytes + = bytesread elif events & (select. POLLERR): p. unregister (fd_dict [fd]) if fd_dict [fd] is fromfd2: if events & (select. POLLIN | select. POLLPRI): bytesread = readwrite (fromfd2, tofd2) totalbytes + = bytesread elif events & (select. POLLERR): p. unregister (fd_dict [fd]) if bytesread <= 0: break return totalbytesdef main (): fromfd1 = open ("/etc/fstab", "r ") fromfd2 = open ("/root/VMwareTools-8.8.1-528969.tar.gz", "r") tofd1 = open ("/root/fstab", "w + ") tofd2 = open ("/var/passwd", "w +") totalbytes = copyPoll (fromfd1, tofd1, fromfd2, tofd2) print "Number of bytes copied % d \ n" % totalbytes return 0if _ name __= = "_ main _": main ()

I hope this article will help you with Python programming.

Related Article

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.