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 将对象设置为可迭代的两种实现方法

1、实现 __getitem__(self) class Library(object): def __init__(self): self.value=['a','b'...

python修改文件内容的3种方法详解

这篇文章主要介绍了python修改文件内容的3种方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 一、修改原文件方式 def...

Python查找第n个子串的技巧分享

Problem Python中str类自带的find、index方法可以返回第一个匹配的子串的位置,但是如果实际使用中需要查找第2个甚至第n个子串的位置该怎么办呢。也许有的码友可能会用到...

Python 实现淘宝秒杀的示例代码

新手学习Python,之前在网上看见一位朋友写的40行Python代码搞定京东秒杀,想在淘宝上帮女朋友抢玩偶,所以就照猫画虎的写了下淘宝的秒杀脚本,经自己实验可行。直接上代码: #-...

python静态方法实例

本文实例讲述了python静态方法。分享给大家供大家参考。 具体实现方法如下: 复制代码 代码如下:staticmethod Found at: __builtin__ staticme...