这里的IO是指网络IO

python中通过select模块实现IO多路复用,select模块中有select、poll、epoll等方法

下面例子以select模块实现IO多路复用

仅仅只有IO多路复用只能实现伪并发

服务器端

#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'zhoufeng'

import socket
import select
sk=socket.socket()
sk.bind(('127.0.0.1',9999,))
sk.listen(5)

inputs=[sk,]  #监听的对象
while True:
    # rlist中元素是socket对象
    # 监听sk(服务器端)对象,如果sk对象发生变化,表示有客户端来连接了,此时rlist值为[sk]
    # 监听conn对象,如果conn发生变化,表示客户端有新消息发送过来了,此时rlist的值为 [客户端]
    rlist,w,e=select.select(inputs,[],[],1)
    #print(rlist)
    print(len(inputs),len(rlist))

    for r in rlist:  #如果rlist列表为空,此for循环不会执行
        if r==sk:  #表示有新的客户端来连接
            #print(r)
            conn,addr=r.accept()   #为新的客户端创建conn对象
            inputs.append(conn)    #将新的conn对象放到inputs中
            conn.sendall(bytes('hello',encoding='utf-8'))
        else:
            r.recv(1024)   #表示客户端发送了数据

客户端

#!/usr/bin/env python
# -*- coding:utf-8 -*-
__author__ = 'zhoufeng'

import socket
import select

sk=socket.socket()
sk.connect(('127.0.0.1',9999,))

data=sk.recv(1024)
print(data)

while True:
    inp=input('>>>')
    sk.sendall(bytes(inp,encoding='utf-8'))

sk.close()

python---IO多路复用的更多相关文章

  1. Python——IO多路复用之select模块epoll方法

    Python——IO多路复用之select模块epoll方法 使用epoll方法实现IO多路复用,使用方法基本与poll方法一致,epoll效率要高于select和poll. .├── epoll_c ...

  2. Python——IO多路复用之select模块poll方法

    Python——IO多路复用之select模块poll方法 使用poll方法实现IO多路复用 .├── poll_client.py├── poll_server.py└── settings.py ...

  3. Python——IO多路复用之select模块select方法

    Python——IO多路复用之select模块select方法 使用select模块的select方法实现Python——IO多路复用 实现同时将终端输入的文本以及客户端传输的文本写入文本文件中: w ...

  4. Python IO 多路复用 \协程

    IO 多路复用 作用:  检测多个socket是否已经发生变化(是否已经连接成功/是否已经获取数据) 即(可读/可写) IO请求时 解决并发  :  单线程 def get_data(key): cl ...

  5. python IO 多路复用

    一.epoll epoll 参考链接: https://www.cnblogs.com/Alanpy/articles/5125986.html epoll  参考链接: https://www.cn ...

  6. Python IO多路复用select模块

    多路复用的分析实例:服务端.客户端 #服务端配置 from socket import * import time import select server = socket(AF_INET, SOC ...

  7. python IO多路复用版FTP

    需求: 实现文件上传及下载功能 支持多连接并发传文件 使用select or selectors

  8. Python(七)Socket编程、IO多路复用、SocketServer

    本章内容: Socket IO多路复用(select) SocketServer 模块(ThreadingTCPServer源码剖析) Socket socket通常也称作"套接字" ...

  9. python中的IO多路复用

    在python的网络编程里,socetserver是个重要的内置模块,其在内部其实就是利用了I/O多路复用.多线程和多进程技术,实现了并发通信.与多进程和多线程相比,I/O多路复用的系统开销小,系统不 ...

  10. socket_server源码剖析、python作用域、IO多路复用

    本节内容: 课前准备知识: 函数嵌套函数的使用方法: 我们在使用函数嵌套函数的时候,是学习装饰器的时候,出现过,由一个函数返回值是一个函数体情况. 我们在使用函数嵌套函数的时候,最好也这么写. def ...

随机推荐

  1. Json格式应用

    Json格式在用于数据存储方面比xml有着空间上的优势,Json格式又主要分为两种格式:名称/值 对 和数组. 在我的业务环境中需要先把一种空间比较小的格式. 测试如下: 取数据库中的一张表然后生成两 ...

  2. Imagenet tools install on windows

    1.find the pyrcc4.exe path: C:\Anaconda2\Library\bin 2.cmd: pyrcc4 -o resources.py resources.qrc 3.a ...

  3. 让CSS布局更加直观:box-sizing

    让CSS布局更加直观:box-sizing 如果你写过CSS或者你接触过CSS,相信你一定对盒子模型一点都不陌生.CSS其中一个让人比较困惑的地方就在于它的盒子模型中关于高度和宽度的计算,别说那些初学 ...

  4. TIOBE Index for January 2016(转载)

    Java has won the TIOBE Index programming language award of the year. This is because Java has the la ...

  5. Events with Dojo(翻译)

    In this tutorial, we will be exploring dojo/on and how Dojo makes it easy to connect to DOM events. ...

  6. ruby 生成有条件限制的随机数

    #conding:utf-8 #生成只有数字的随机码可控制长度def random_int(len) newpass = "" 1.upto(len){ |i| newpass & ...

  7. JAVA正则表达式:Pattern类与Matcher类详解(转)

    java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包.它包括两个类:Pattern和Matcher Pattern 一个Pattern是一个正则表达式经编译后的表 ...

  8. 理解Condition的用法

    这个示例中BoundedBuffer是一个固定长度的集合,这个在其put操作时,如果发现长度已经达到最大长度,那么会等待notFull信号,如果得到notFull信号会像集合中添加元素,并发出notE ...

  9. Unity UI on the HoloLens

    Following the steps under "Required configuration" will allow Unity UI to continue to work ...

  10. debian下安装zendframework

    第一步,打开apache的rewrite模块,因为在debian下使用apache必须执行这一步 a2enmod rewrite #激活rewrite模块 /etc/init.d/apache2 re ...