这里的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. android源码的目录结构

    android源码的目录结构 [以下网络摘抄] |-- Makefile ! l/ a5 n% S% @- `0 d# z# a$ P4 V3 o7 R|-- bionic              ...

  2. IT荐书|10个最“牛叉”的代码注释

    下面是 网友针对“你看到过的最好的代码注释是什么样的?”这个问题给出的回答的前10条: 1. // 亲爱的维护者: // 如果你尝试了对这段程序进行‘优化’, // 并认识到这种企图是大错特错,请增加 ...

  3. ajax-向服务器发送请求

    ajax-向服务器发送请求 1.将请求发送到服务器,使用XMLHttpRequest对象的 open() 和 send() 方法.     xmlhttp. open(method,url,async ...

  4. 为了体验 ILS 在Win2012R2 Hyper-V上安装Windows 2000 AdvSer

    Win2012 R2 Hyper-V 的集成服务包已不支持Windows 2000 先安装SP4.IE6.更新汇总包,再来安装这个. 安装完成后,会有几个未知设备,直接禁用就可以了. 开启Window ...

  5. Date和Calendar时间操作常用方法及示例

    package test; import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date; /** ...

  6. Oracle 记录插入时“Invalid parameter binding ”错误

    出现这种错误的原因可能有一下几种: 由于OracleParameter[] parameters:中parameters的个数和对应的插入SQL语句中的冒号个数不等: 参数个数和冒号个数相等,但是如下 ...

  7. 如何用iframe在网页中插入另一个网页的一部分内容,做成页中页

    <html><head></head><body><h1>这是一段引用的内容!!!</h1><div style=&quo ...

  8. NPOI Excel 单元格背景颜色对照表

    NPOI Excel 单元格颜色对照表,在引用了 NPOI.dll 后可通过 ICellStyle 接口的 FillForegroundColor 属性实现 Excel 单元格的背景色设置,FillP ...

  9. Career path of Bioinformatics

    Core services: Reward bioinformaticians http://www.nature.com/news/core-services-reward-bioinformati ...

  10. C++ 几种构造函数的区分和调用

    class A { public: A(); //默认构造 A( A& a ); //copy构造 const A& operator=( const A& a ); //赋值 ...