Python通过poll实现异步IO的方法

yipeiwu_com7年前Python基础

本文实例讲述了Python通过poll实现异步IO的方法。分享给大家供大家参考。具体分析如下:

在使用poll()后返回轮询对象,该对象支持以下方法:
pollObj.register(fd,[,eventmask])第一个参数是注册新的文件描述符fd,fd要么是一个整数文件描述符,要么可以带有一个获取文件描述符的fileno()方法的对象。eventmask是一些按位或标记,这些标记指示要处理的事件。

POLLIN:       用于读取数据
POLLPRI:      用于读取紧急数据
POLLOUT:      准备写入
POLLERR:      错误情况
POLLHUP:      保持状态
POLLNVAL:     无效请求

最后在循环中利用pollObj.poll()来进行对已注册的文件描述符进行轮询。返回一元祖(fd,event)。其中fd是文件描述符,event是指示时间的位掩码。至需要将event与对应的时间进行&测试即可。

利用poll创建对一个多路文件复制程序,代码如下:

#!/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,fromfd2.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 %d\n" % totalbytes
  return 0
if __name__=="__main__":
  main()

希望本文所述对大家的Python程序设计有所帮助。

相关文章

利用python求积分的实例

python的numpy库集成了很多的函数。利用其中的函数可以很方便的解决一些数学问题。本篇介绍如何使用python的numpy来求解积分。 代码如下: # -*- coding:...

让你的Python代码实现类型提示功能

Python是一种动态类型语言,这意味着我们在编写代码的时候更为自由,但是与此同时IDE无法向静态类型语言那样分析代码,及时给我们相应的提示。为了解决这个问题,Python 3.6 新增...

wxPython色环电阻计算器

本文实例为大家分享了wxPython色环电阻计算器的具体代码,供大家参考,具体内容如下 import wx # 导入wxPython class MyFrame(wx.Frame):...

python万年历实现代码 含运行结果

python万年历实现代码 含运行结果

本文实例为大家分享了python实现万年历的具体代码,供大家参考,具体内容如下 #coding:utf-8 def leap_year(year):#判断平瑞年 if year...

python自定义函数实现一个数的三次方计算方法

python自定义函数实现一个数的三次方计算方法

python自定义函数在运行时,最初只是存在内存中,只有调用时才会触发运行。 def cube_count(a): if is_number(a): return a**...