Python's method of implementing asynchronous IO via poll

Source: Internet
Author: User

The example in this article describes how Python implements asynchronous Io via poll. Share to everyone for your reference. The specific analysis is as follows:

Returns a polling object after using poll (), which supports the following methods:

Pollobj.register (Fd,[,eventmask]) The first argument is to register a new file descriptor fd,fd either an integer file descriptor or an object with a Fileno () method that gets the file descriptor. Eventmask are bits or tags that indicate the events to be handled.

Pollin: for reading data

POLLPRI: for reading emergency data

Pollout: Preparing to write

Pollerr: Error condition

Pollhup: Keep the state

Pollnval: Invalid request

Finally, the Pollobj.poll () is used in the loop to poll the registered file descriptor. Returns a unary ancestor (fd,event). Where FD is a file descriptor, the event is a bitmask indicating the time. The event and the corresponding time should be & tested.

Using poll to create a multiple-path file replicator, the code is as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the #!/usr/bin/env python Import select blksize=8192 def readwrite (fromfd,tofd): Readbuf = Fromfd.read (blksize) if readbuf: Tofd.write (READBUF) Tofd.flush () return Len (READBUF) def copypoll (fromfd1,tofd1,fromfd2,tofd2): #定义需要监听的事件 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 (): FROMFD1,FROMFD 2.fileno (): Fromfd2} #创建poll对象p p=select.poll () #利用poll对象p对需要监视的文件描述符进行注册 p.register (fromfd1,read_only) P.register ( fromfd2,read_only) while True: #轮询已经注册的文件描述符是否已经准备好 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 totalbytes def 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 "%%dn return 0 If __name__== "__main__": Main ()

I hope this article will help you with your 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.