设为首页 收藏本站
查看: 1088|回复: 0

[经验分享] python select模块.select实现非阻塞socket,Popen等(特别在 Unix 下,它还可以用于管道)

[复制链接]

尚未签到

发表于 2017-5-8 11:29:52 | 显示全部楼层 |阅读模式
link: http://blog.sina.com.cn/s/blog_76a0e17a0100q762.html

select — Waiting for I/O completion


This module provides access to the select() and poll() functions available in most operating systems. Note that on 
Windows
, it only works for sockets; on other operating systems, it also works for other file types (in particular, on Unix, it works on pipes). It cannot be u
sed
 on regular files to determine whether a file has grown since it was last read.

这个模块提供的 select() 和 poll() 方法可用于大部分
操作系统
。注意:在 
Win
dows 下,它只能用于 sockets ;在其他
系统
下,它也可以用于其他类型(特别在 Unix 下,它还可以用于管道)。它不能用来确定是否完成上次的对普通
文件
执行的读取操作。


The module defines the following:

这个模块定义下列内容:


exception error

异常错误

The exception raised when an error occurs. The accompanying value is a pair containing the numeric error code from errno and the corresponding string, as would be printed by the C function perror().

在发生错误将引发异常。accompanying 值包含错误代码和错误描述文本,用于使用 C 程序 perror() 打印。


poll( )

(Not supported by all operating systems.) Returns a polling object, which supports registering and unregistering file descriptors, and then polling them for I/O events; see section 15.1.1 below for the methods supported by polling objects.


select( iwtd, owtd, ewtd[, timeout])

This is a straightforward interface to the Unix select() system call. The first three arguments are sequences of `waitable objects’: either integers representing file descriptors or objects with a parameterless method named fileno() returning such an integer. The three sequences of waitable objects are for input, output and `exceptional conditions’, respectively. Empty sequences are allowed, but acceptance of three empty sequences is platform-dependent. (It is known to work on Unix but not on Windows.) The optional timeout argument specifies a time-out as a floating point number in seconds. When the timeout argument is omitted the function blocks until at least one file descriptor is ready. A time-out value of zero specifies a poll and never blocks.

这对于 Ubix 系统调用 select() 来说是一个简单接口。前三个参数是‘等待对象’;????。这三个参数分别按顺序等待输入、输出输出和异常状态。允许空数组,但是是否允许三个空序列依赖于
平台
。(已知支持 Unix 但是不支持 Windows。)timeout 参数接受浮点数表示的秒。如果省略 timeout 着阻塞到至少一个文件描述符准备就绪。timeout 为0表示从不阻塞。

译者注:在对方关闭连接的情况下(CLOSE_WAIT状态)会立即返回,并且处于可读列表里面。但是却会立即读到0长度的
数据
。建议在检测到连续多次读到0长度数据时关闭连接。


The return value is a triple of lists of objects that are ready: subsets of the first three arguments. When the time-out is reached without a file descriptor becoming ready, three empty lists are returned.

返回值是包含前三个参数里面已准备就绪的对象的3个列表.如果已经超时但是并没有对象准备就绪,那么返回3个空列表


Among the acceptable object types in the sequences are Python file objects (e.g. sys.stdin, or objects returned by open() or os.popen()), socket objects returned by socket.socket().You may also define a wrapper class yourself, as long as it has an appropriate fileno() method (that really returns a file descriptor, not just a random integer). Note: File objects on Windows are not acceptable, but sockets are. On Windows, the underlying select() function is provided by the WinSock library, and does not handle file descriptors that don’t originate from WinSock.

接受的对象类型按次序是 Python 文件对象(例如:sys.stdin 或者 open()或os.popen()返回的对象)或者socket.socket()返回的socket对象。你也可以定义一个类,只要它包含恰当的 fileno() 方法(它其实范围文件描述符,不要只是返回随机数)注意:windows 不接受文件对象,但是接受 sockets。在 windows 下 select() 程序隐式使用 WinSock 库,WinSock 并不支持文件对象句柄。



select.select(rlist, wlist, xlist[, timeout])

> 平台依赖性:

 Windows只支持socket作为fd,不支持文件的fd

 Linux 和Unix平台都支持

> 输入的文件描述符号可以为如下几种情况:

python file objects: 

              1. sys.stdin  

              2. open()   或者 os.popen()

              3. socket.socket()

              4. 其他可以调用fileno()的对象返回的文件描述符号(这个是真实的 fd,不仅仅是一个随机整数)


> 前三个参数是 ‘waitable objects’的序列,要么是整数代表的fd,或者是由fileno()返回的对象才能作为三个序列的元素

rlist: wait until ready for reading
wlist: wait until ready for writing
xlist: wait for an “exceptional condition” (see the manual page for what your system considers such a condition)

三个参数是否全为空是平台相关的,Unix系统是可以全为空的,Windows不能全为空.

Unix全为空的话,可以作为定时器用了。

> timeout 参数

timeout参数是浮点数代表的参数,单位是seconds

timeout参数省略(默认为None),则代表永远block直到有一个fd准备好。

timeout = 0, 代表不会block,或者(specifies a poll)

> return 参数

返回值为三个list,包含三个已经准备好的fd列表(不会超过输入三个参数)。

当timeout后还没有fd准备好,则返回三个空列表([],[],[])

python 的 List Comprehensions语法:

这个语法的目的是用于创建列表,其他相似的语法还有(map,filter, zip 和 lambda)

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-374637-1-1.html 上篇帖子: 使用python开发天气预报查询网(www.tianqiyubao-5-7-10.com)的总结一 下篇帖子: Let's-python视频教程发布专贴(更新下载方式: 纳米,电驴,heyjava)(2010年12月04日更新纳米部分资源)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表