Python select模块学习
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模块学习的更多相关文章
- python - argparse 模块学习
python - argparse 模块学习 设置一个解析器 使用argparse的第一步就是创建一个解析器对象,并告诉它将会有些什么参数.那么当你的程序运行时,该解析器就可以用于处理命令行参数. 解 ...
- python select模块
Python select 一.前言 Python的select()方法直接调用操作系统的IO接口,它监控sockets,open files, and pipes(所有带fileno()方法的文件句 ...
- python paramiko模块学习分享
python paramiko模块学习分享 paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接.paramiko支持Linux, Sola ...
- python select模块详解
要理解select.select模块其实主要就是要理解它的参数, 以及其三个返回值.select()方法接收并监控3个通信列表, 第一个是所有的输入的data,就是指外部发过来的数据,第2个是监控和接 ...
- Python logging 模块学习
logging example Level When it's used Numeric value DEBUG Detailed information, typically of interest ...
- Python time模块学习
Python time模块提供了一些用于管理时间和日期的C库函数,由于它绑定到底层C实现,因此一些细节会基于具体的平台. 一.壁挂钟时间 1.time() time模块的核心函数time(),它返回纪 ...
- python os模块学习
一.os模块概述 Python os模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的. 二.常用方法 1.os.name 输出字符串指示正在使用的平台.如果是wi ...
- python logging模块学习(转)
前言 日志是非常重要的,最近有接触到这个,所以系统的看一下Python这个模块的用法.本文即为Logging模块的用法简介,主要参考文章为Python官方文档,链接见参考列表. 另外,Python的H ...
- python atexit模块学习
python atexit模块 只定义了一个register模块用于注册程序退出时的回调函数,我们可以在这个函数中做一下资源清理的操作 注:如果程序是非正常crash,或者通过os._exit()退出 ...
随机推荐
- 高性能mysql 第五章 索引部分总结
高性能索引 1.索引基础:索引的作用类似'目录'帮助Query来快速定位数据行. 1.1索引类型: 1.1.1 b-tree索引 b-tree(balance tree)索引:使用平衡树(非平衡二叉树 ...
- (整理)SQL Server 2008 CDC 功能使用
最近某项目突然要增加数据的获取,但是不能改程序.也没有同步的只读库,只好使用CDC来进行尝试. CDC的启用和停止全部用SQL实现,在这里给出主要的SQL步骤: /****** Script for ...
- systemverilog assertion
1.一般是单独写一个module 里面放assertion, 然后在验证平台顶层和RTL的实例化bind起来 2. |->表示直接进行判断,|=>表示下一拍判断,一般一个断言最好只写一 ...
- npm镜像地址的修改或切换
方法一:直接编辑npm的配置文件npm config edit修改registry的地址registry=https://registry.npm.taobao.org 方法二:用代码更改npm的配置 ...
- OOM三种情况
第一种OutOfMemoryError: PermGen space发生这种问题的原意是程序中使用了大量的jar或class,使java虚拟机装载类的空间不够,与Permanent Generatio ...
- Idea的pom文件导入依赖包仍然报错
- IOS搜索框输入中文解决方案(防抖)
class Header extends React.Component { constructor(props) { super(props); this.time = 0; // 重点在于这个th ...
- 吴裕雄 python深度学习与实践(10)
import tensorflow as tf input1 = tf.constant(1) print(input1) input2 = tf.Variable(2,tf.int32) print ...
- tensorflow-serving-gpu 本地编译并使用
因为要部署,模型比较大,所以通常官网的pip install 和bazel 教程编译的都是cpu版本的代码, 所以为了感受下gpu就尝试自己编译了,首先,下载源码: git clone --recur ...
- pyton 模块之 pysmb 文件上传和下载(linux)
首先安装pysmb模块 下载文件 from smb.SMBConnection import SMBConnection conn = SMBConnection('anonymous', '', ' ...