select 是常用的异步socket 处理方法

  一般用法:

    # iwtd,owtd,ewtd 分别为需要异步处理的读socket队列, 写socket队列(一般不用), 和错误socket队列, 返回事件的读写和错误socket队列

il,ol,el = select(iwtd,owtd,ewtd[,timeout])
for sock in il:
#read the sock
for sock in ol:
#...
for sock in el:
#handle errors

  select 和 poll 都是比较低级的函数, 用起来比较麻烦, 如果使用异步socket编程,可以使用twisted

  1.模块的功能主要是等待I/O完成, 提供在大多数操作系统上对select() 和 poll()函数的调用, 在Linux 2.5+上可以调用epoll(),在BSD上可以调用kqueue() , 在Windows上, 模块只能工作在socket上, 在其他操作系统上, 它还可以工作在其他文件类型上(如在Unix上, 可以工作在pipes上).它不能被用来判断一个普通文件在最后一次读操作之后是否有grown.

  模块定义了:

    exception:

      select.error(): 当错误发生时抛出,  会像C函数的perror()一样打印错误编号和描述字符串

    functions:

      select.epoll([sizehint=-1]): 返回一个edge polling 对象, 可以用来作为Edge or Level Triggered interface for I/O  events.

      select.poll(): 不是在所有系统上都支持, 返回一个polling 对象, 可以用来支持registering and unregistering file descriptors, 然后polling them for I/O events.

      select.kqueue(): 只支持BSD, 返回一个kernel queue object.

      select.kevent(ident,filter=KQ_FILTER_READ,flags=KQ_EV_ADD,fflags=0,data=0,udata=0): 只支持BSD,返回一个kernel queue object.

      select.select(rlist,wlist,xlist[,timeout]): 这是一个straightforward interface to Unix select()系统调用, 前3个参数是一串可等待对象, 表示代表文件描述符的整数或者带有一个名为fileno()的无参方法返回的同样的整数;

        参数:  1.rlist: 等待直到读准备好; 2.wlist: 等待直到写操作准备好; 3.xlist: 等待一个"exceptional condition" ;      允许空序列, 但是如果3个参数都为空的列表的话, 在Unix上可以, 但在Windows上不行, 与平台相关 . 当timeout参数被设定之后, 函数将blocks 知道至少一个文件描述符 is ready, 值为0 表示 a poll and never block.

        返回值: triple of list of object that are ready. subsets of the first three arguments. 当time-out reached, 但还没有file descriptor ready, 返回 three empty lists.

        Among the acceptable object types in the sequence are python file object, like sys.stdin or objects returned by open() or os.popen()(这个命令可以用来执行系统调用, 相当于os.system(), 用法: os.popen("ping 192.168.1.1")) .

    Constant:

      select.PIPE_BUF: ...

  2. Edge and Level Trigger Polling(epoll) Object

    

  epoll.close() : close the control file descriptor of the epoll object.  

  epoll.fileno(): return the file descriptor number of the control fd

  epoll.fromfd(fd): create an epoll object from a given file descriptor

  epoll.register(fd[,eventmask]) : register a fd descriptor with the epoll object.

  更多信息参考: https://docs.python.org/2.7/library/select.html?highlight=select#module-select

  

      

Python select模块学习的更多相关文章

  1. python - argparse 模块学习

    python - argparse 模块学习 设置一个解析器 使用argparse的第一步就是创建一个解析器对象,并告诉它将会有些什么参数.那么当你的程序运行时,该解析器就可以用于处理命令行参数. 解 ...

  2. python select模块

    Python select 一.前言 Python的select()方法直接调用操作系统的IO接口,它监控sockets,open files, and pipes(所有带fileno()方法的文件句 ...

  3. python paramiko模块学习分享

    python paramiko模块学习分享 paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接.paramiko支持Linux, Sola ...

  4. python select模块详解

    要理解select.select模块其实主要就是要理解它的参数, 以及其三个返回值.select()方法接收并监控3个通信列表, 第一个是所有的输入的data,就是指外部发过来的数据,第2个是监控和接 ...

  5. Python logging 模块学习

    logging example Level When it's used Numeric value DEBUG Detailed information, typically of interest ...

  6. Python time模块学习

    Python time模块提供了一些用于管理时间和日期的C库函数,由于它绑定到底层C实现,因此一些细节会基于具体的平台. 一.壁挂钟时间 1.time() time模块的核心函数time(),它返回纪 ...

  7. python os模块学习

    一.os模块概述 Python os模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的. 二.常用方法 1.os.name 输出字符串指示正在使用的平台.如果是wi ...

  8. python logging模块学习(转)

    前言 日志是非常重要的,最近有接触到这个,所以系统的看一下Python这个模块的用法.本文即为Logging模块的用法简介,主要参考文章为Python官方文档,链接见参考列表. 另外,Python的H ...

  9. python atexit模块学习

    python atexit模块 只定义了一个register模块用于注册程序退出时的回调函数,我们可以在这个函数中做一下资源清理的操作 注:如果程序是非正常crash,或者通过os._exit()退出 ...

随机推荐

  1. ijkplayer总结

    12.ijkplayer的使用过程: 11.ijkpalyer引言: ==== 12.ijkplayer的使用过程: >>举例mac系统编译.so文件:   ijkplayer默认是不支持 ...

  2. Lepus监控之安装部署

    PHP和Python都是跨平台的语言,所以理论上系统应该可以支持在不同的平台上运行.但是由于时间和精力以及资源有限,目前天兔系统只测试完善了Centos/RedHat系统的支持.我们目前提供的技术支持 ...

  3. Reachability实时监控网络变化

    Reachability是一种实时观察网络发生变化控件,如当你的手机处于WiFi情况下,他就会检测环境,当处于GPS的情况下改变环境,当处于无网络的情况下又是一种环境.下面我们看下关于Reachabi ...

  4. Phpstorm 与 服务器 同步 代码

    链接:(自己领悟就好了) Phpstorm同步代码

  5. 有哪些你不知道的python小工具

    python作为越来越流行的一种编程语言,不仅仅是因为它语言简单,有许多现成的包可以直接调用. python中还有大量的小工具,让你的python工作更有效率. 1.- 快速共享 - HTTP服务器 ...

  6. python变量、注释、程序交互、格式化输入、基本运算符

    变量                                                                                                  ...

  7. python tp_ready函数分析

    int PyType_Ready(PyTypeObject *type) { PyObject *dict, *bases; PyTypeObject *base; Py_ssize_t i, n; ...

  8. React将某段文字插入到某个元素里

    最基本使用: 引入依赖文件: <script src="https://unpkg.com/react@16/umd/react.development.js">< ...

  9. ElasicSearch(4) 与jest结合

    https://spring.io/projects/spring-data-elasticsearch https://docs.spring.io/spring-data/elasticsearc ...

  10. CSS样式整理大全

    转载自:http://www.cnblogs.com/laihuayan/archive/2012/07/27/2611111.html 字体属性:(font) 大小 {font-size: x-la ...