`

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

 
阅读更多

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)

分享到:
评论

相关推荐

    python标准库3.7.3.docx

    - **1.3 exceptions 模块**:这个模块在早期版本的Python中用于定义异常类,但在Python 3中已经被整合到Python语言本身。 - **1.4 os 模块**:提供了一组与操作系统交互的功能,例如文件管理、环境变量访问等。 - **...

    Python标准库中文版.pdf

    Python标准库是Python编程语言的一个重要组成部分,它为Python提供了丰富的模块,使得开发人员能够不必自行编写所有功能,而是通过使用标准库中的模块来实现各种功能。从给定文件的内容来看,该文档是关于Python标准...

    python3.6.5参考手册 chm

    Python参考手册,官方正式版参考手册,chm版。以下摘取部分内容:Navigation index modules | next | Python » 3.6.5 Documentation » Python Documentation contents What’s New in Python What’s New In ...

    python标准库

    array模块提供了数组支持,相比于list,它可以存储单一类型的元素,并且在某些情况下性能更好。 #### 4.2 struct模块 struct模块提供了将Python值打包成C语言结构体的支持,对于二进制数据处理非常有用。 #### 4.3...

    python标准库-书籍,原版-高清可复制

    - popen2模块:提供popen函数的不同变体,用于创建管道进行进程间通信。 - signal模块:提供信号处理功能,用于进程间通信。 ### 4. 数据表示 用于在Python中表示特定类型的数据。 - array模块:提供固定类型数组...

    MySQL中用通用查询日志找出查询次数最多的语句的教程

    Tcp port: 3306 Unix socket: /home/mysql/logs/mysql.sock TimeId Command Argument ***:56:44 3 Query select * from mysql.general_log ``` 一旦开启了通用查询日志,您可能会想要分析日志,找出查询次数最多的...

Global site tag (gtag.js) - Google Analytics