参数文件描述符[Python]Socket高级 select I/O复用模型(一)

上班之余抽点时光出来写写博文,希望对新接触的朋友有帮助。今天在这里和大家一起学习一下参数文件描述符

    这个货色之前刚接触,有很多解理不是很楚清。对于模型和不同模型的比较,unix网络程编 有细详的释解

    

    因为python是简略用调unix系统的函数,所以找了unix网络程编参看了下,还是比拟糊模 

    select 是属于同步I/O作操,属于I/O复用模型的一种。

    这个函数许允进程示指核内待等多个事件中的任一个生发,并仅在一个或多个事件生发或经过某指定的时光后才唤醒进程 

    模型如下图  recvfrom 是系统用调

    

    

    

    文档中这么述描的

    select.select(rlist, wlist, xlist[, 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:
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

    
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.
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.
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.

文档中的大致意思:
这是一个直接用调unix中select()的简略口接。前三个参数都是‘待等象对’的序列:整型的文件述描符或者是一个无参数法方fileno()返回的整数:
* rlist 待等直到预备好写
* wlist 待等直到预备好读
* xlist 待等一种外意的情况 (在手册中查看你的系统中为认的那种情况)
空序列也是许允的,但是能不能3个参数都为空就要由你的系统定决了。(众所都知unix下是行得,windows下不行)timeout指定了一个秒级的浮点型参数表现时超时光当timeout参数为空的时候省略了函数会阻塞直到至少有一个文件述描符经已预备好了。
返回值是三个经已预备好的列表,也是3个参数象对的子集。如果时超了,返回的是三个空列表。其中列表中可以接收的参数型类是Python中的文件参数(例如 sys.stdin 或者是 open() sys.popen()的返回象对),或者是 socket.socket的返回象对。你也可以自己封装成一个类,只要合适fileno()法方 
note:在windows中文件象对是法无接受的,但是socket是可以应用的。

    每日一道理
航行者把树比作指引方向的路灯,劳动者把树比作遮风挡雨的雨伞,诗人把树比作笔下的精灵,而我却要把树比作教师,它就是为我们遮风挡雨的伞,指明方向的路灯,打开知识殿堂的金钥匙。

    这里并没有道理的等的明说

    

    其他网络资料

    ref:  http://www.cnblogs.com/coser/archive/2012/01/06/2315216.html

    

 

    

  • select 
  • select 
  • select最早于1983年出现在4.2BSD中,它通过一个select()系统用调来监视多个文件述描符的数组,当select()返回后,该数组中就绪的文件述描符便会被核内改修标志位,使得进程可以获得这些文件述描符从而行进后续的写读作操。
  • select现在几乎在有所的平台上支撑,其精良跨平台支撑也是它的一个点优,事实上从现在看来,这也是它所剩多不的点优之一。
  • select的一个缺陷在于单个进程可以监视的文件述描符的数量存在最大制约,在Linux上一般为1024,不过可以通过改修宏定义甚至重新编译核内的式方升提这一制约。
  • 另外,select()所维护的存储大批文件述描符的数据结构,随着文件述描符数量的增大,其复制的开销也线性长增。同时,由于网络响应时光的迟延使得大批TCP连接处于非活泼状态,但用调select()会对有所socket行进一次线性扫描,所以这也浪费了定一的开销。

    


    

基本是一些观点的述描

    


    

代码是程课中着跟师老写的,弄懂了再整顿。

文章结束给大家分享下程序员的一些笑话语录: 自从有了Photoshop,我再也不相信照片了!(没有Photoshop的年代,胶片照片年代做假的也不少,那时候都相信假的!)

原文地址:https://www.cnblogs.com/xinyuyuanm/p/3035988.html