select是全平台通用的IO多路复用模块。最大连接数:1024。

poll和epoll没有最大连接数限制,但只能用在linux平台。

selectors是再封装模块,推荐使用。下篇会讨论。

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:

    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

    • 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)

方法、属性 参数 作用 示例
select(rlist,wlist,rlist,[timout=1])
poll() 没人用了,已经升级为epoll
epoll(sizehint = -1,flags=0)

sizehint informs epoll about the expected number of events to be registered.  It must be positive, or-1to use the default. It is only used on older systems where epoll_create1() is not available; otherwise it has no effect (though its value is still checked).

flags is deprecated and completely ignored.  However, when supplied, its value must be 0 or select.EPOLL_CLOEXEC, otherwise OSError is raised.

(Only supported on Linux 2.5.44 and newer.) Return an edge polling object, which can be used as Edge or Level Triggered interface for I/O events.
devpoll() (Only supported on Solaris and derivatives.)  Returns a /dev/poll polling object; see section /dev/poll Polling Objects below for the methods supported by devpoll objects.
kevent() select.kevent(ident, filter=KQ_FILTER_READ, flags=KQ_EV_ADD, fflags=0, data=0, udata=0

  • (Only supported on BSD.)  Returns a kernel event object; see section Kevent Objects below for the methods supported by kevent objects.

kqueue() (Only supported on BSD.)  Returns a kernel queue object; see section Kqueue Objects below for the methods supported by kqueue objects.
import socket
import os
import select
import queue
import jsonclass SelectFTP(object):def __init__(self, ip, port):self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)self.server.setblocking(0)self.server.bind((ip, port))self.server.listen(20)self.inputs = [self.server]self.outputs = []self.file_attr = {}# file_attr format:file_attr[socket]:{func:'', filename:'', len:999, recv_len:0}      self.socket_queue = {}def upload(self, sock, write_data):# if os.path.isfile(self.file_attr[sock]['filename']):with open(self.file_attr[sock]['filename'], 'a+') as file:file.write(data)self.file_attr[sock][recv_len] += len(write_data)if self.file_attr[sock][recv_len] == self.file_attr[sock][len]:del self.file_attr[sock]file.close()def download(self, sock, *args):passdef run(self):while self.inputs:read_active, read_output, exception = select.select(self.inputs, self.outputs, self.inputs)for fd in read_active:if fd is server:conn, addr = fd.accept(1024)conn.setblocking(0)self.inputs = self.inputs.append(conn)self.socket_queue[fd] = queue.Queue()else:recv_data = fd.recv(1024)if recv_data:data = json.loads(recv_data.decode())if fd not in self.file_attr.keys:self.file_attr[fd] = dataelse:try:self.socket_queue.put_nowait(data)if fd not in self.outputs:self.outputs.append(fd)except Exception as e:print(e)else:self.inputs.remove(fd)if fd in self.outputs:self.outputs.remove(fd)del self.socket_queue[fd]send_data = for fd in read_output:try:message = self.socket_queue.get_nowait()except queue.Empty:self.outputs.remove(fd)print('wait...')else:getattr(self.file_attr[fd]['func'])(fd, message)

转载于:https://blog.51cto.com/yishi/2150318

【select模块】select IO多路复用和select实现FTP相关推荐

  1. 【python】-- IO多路复用(select、poll、epoll)介绍及实现

    IO多路复用(select.poll.epoll)介绍及select.epoll的实现 IO多路复用中包括 select.pool.epoll,这些都属于同步,还不属于异步 一.IO多路复用介绍 1. ...

  2. Python之进程+线程+协程(事件驱动模型、IO多路复用、select与epoll)

    文章目录 一.事件驱动模型 二.IO多路复用 本篇文章是关于涉及网络编程与协程.进程之间结合的内容,其中事件驱动模型.IO多路复用.select与epoll的使用等方面的知识 一.事件驱动模型 1.事 ...

  3. 聊聊IO多路复用之select、poll、epoll详解

    聊聊IO多路复用之select.poll.epoll详解 2016/04/22 · IT技术 · 1 评论 · epoll, IO多路复用, poll, select 分享到:0 本文作者: 伯乐在线 ...

  4. IO多路复用之select全面总结(必看篇)

    转载:http://www.jb51.net/article/101057.htm 1.基本概念 IO多路复用是指内核一旦发现进程指定的一个或者多个IO条件准备读取,它就通知该进程.IO多路复用适用如 ...

  5. IO多路复用中select、poll、epoll之间的区别

    本文来说下IO多路复用中select.poll.epoll之间的区别 文章目录 什么是IO多路复用 为什么有IO多路复用机制 同步阻塞(BIO) 同步非阻塞(NIO) IO多路复用(现在的做法) 3种 ...

  6. python io多路复用_【python】-- IO多路复用(select、poll、epoll)介绍及实现

    IO多路复用(select.poll.epoll)介绍及select.epoll的实现 IO多路复用中包括 select.pool.epoll,这些都属于同步,还不属于异步 一.IO多路复用介绍 1. ...

  7. Linux IO多路复用之Select简史

    内容目录 前言早期的UnixTCP/IP诞生后终端复用套接字章节回顾结论引用 前言 最近我一直在思考 Linux 中的多路复用,即 epoll(7)[1]系统调用.我很好奇 epoll与Windows ...

  8. IO多路复用之select篇

    1.基本概念 IO多路复用是指内核一旦发现进程指定的一个或者多个IO条件准备读取,它就通知该进程.IO多路复用适用如下场合: (1)当客户处理多个描述字时(一般是交互式输入和网络套接口),必须使用I/ ...

  9. html select选择事件_一道搜狗面试题:IO多路复用中select、poll、epoll之间的区别...

    (1)select==>时间复杂度O(n) 它仅仅知道了,有I/O事件发生了,却并不知道是哪那几个流(可能有一个,多个,甚至全部),我们只能无差别轮询所有流,找出能读出数据,或者写入数据的流,对 ...

最新文章

  1. [修订版]”大脑“爆发背后是50年互联网架构重大变革
  2. python三层装饰器-python中自带的三个装饰器的实现
  3. JAVA防盗链在报表中的应用实例
  4. php7与php5的区别,PHP7和PHP5区别
  5. 头指针与头结点的异同
  6. 工具--常见eclipse配置导入web工程(tomcat容器)步骤
  7. mysql ignore-columns_坑 - 当insert ignore遇到not null
  8. 数据库系统:NoSQL与SQL的区别
  9. Java之品优购课程讲义_day06(1)
  10. 学生管理系统总结收获——限制字符
  11. 修改BT种子的tracker服务器list
  12. 下一代计算机 激光,《Nature》:仅需一束激光,计算机速度有望能再快100万倍...
  13. 谈谈我们为什么要前后端分离
  14. tp5使用mpdf生成pdf文件时,碰到division by zero问题解决记录
  15. Laravel——微信授权登陆
  16. Android绘制几何图形详解
  17. onlyoffice 收费不_OMG!你家小区物业有没有这些乱收费的现象……|物业|物业管理|物业服务|门禁卡|停车费...
  18. Mysteel解读:2022年1-10月份马铃薯淀粉进口数据分析
  19. 比较好用的CDN加速节点
  20. 错误1053 服务没有及时响应启动或控制请求

热门文章

  1. vivo应用商店电脑版_VIVO应用商店代理商江湖的那些关系
  2. GPU Gems2 - 4 分段缓冲(Segment Buffering)
  3. 第一章:线性空间和线性变换
  4. python学习,day3:函数式编程,*arge,**kwargs
  5. jquery 下拉框 select2 运用 笔记
  6. Centos6.5静态IP设置
  7. iOSPush自动隐藏tabbar
  8. sqlserver 查询中使用Union或Union All
  9. dbgrideh的功能
  10. ogre plugin for 3dmax 最新进度和功能说明