server coding:

 #!/usr/bin/python
# -*- coding: utf-8 -*- import select
import socket
import sys
import Queue # Create a TCP/IP socket
server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.setblocking(False) # Bind the socket to the port
server_address = ('localhost',1000)
print >>sys.stderr, 'starting up on %s port %s'%server_address
server.bind(server_address) # Listen for incoming connections
server.listen(5) # Sockets from which we expect to read
inputs = [server] # Sockets to which we expect to write
outputs = [] # Outgoing message queues (socket:Queue)
message_queues = {} while True:
# Wait for at least one of the sockets to be ready for processing
print >>sys.stderr, '\nwaiting for the next event'
#分别筛选出可接收消息的sockets、等待发送消息的sockets、和中途出错的sockets。
#其中exceptional为了把所有可接收消息的sockets都遍历到,所以要从inputs列表中读取。
readable, writable, exceptional = select.select(inputs,outputs,inputs) # Handle inputs
for s in readable:
#第一种情况,创建一个待接收消息的socket放入inputs
if s is server:
# A "readable" server socket is ready to accept a connection
connection, client_address = s.accept()
print >>sys.stderr, 'new connection from', client_address
connection.setblocking(0)
inputs.append(connection) # Give the connection a queue for data we want to send
message_queues[connection] = Queue.Queue() #第二种情况,在后续循环中,已经添加到inputs中的sockets已经不是readable了,所以
#要进行接收消息,消息存在message_queues中,并把该socket添加到outputs。
else:
data = s.recv(1024)
if data:
# A readable client socket has data
print >>sys.stderr, 'receieved "%s" from %s'%(data.upper(),s.getpeername())
message_queues[s].put(data)
# Add output channel for response,添加到待发送消息列表
if s not in outputs:
outputs.append(s) #第三种情况就是这个客户端已经断开了,
#所以你再通过recv()接收到的数据就为空了,
#所以这个时候你就可以把这个跟客户端的连接关闭了。
else:
print >>sys.stderr, 'closing',client_address,'after reading no data'
if s in outputs:
outputs.remove(s)#既然客户端都断开了,我就不用再给它返回数据了,所以这时候如果这个客户端的连接对象还在outputs列表中,就把它删掉
inputs.remove(s)#inputs中也删除掉
s.close()#把这个连接关闭掉 # Remove message queue
del message_queues[s] # Handle outputs
for s2 in writable:
try:
next_msg = message_queues[s2].get_nowait()
#没有消息了
except Queue.Empty:
# No messages waiting so stop checking for writability.
print >>sys.stderr,'output queue for',s2.getpeername(),'is empty'
outputs.remove(s2)
#发送消息
else:
print >>sys.stderr, 'sending "%s" to %s'%(next_msg,s2.getpeername())
s2.send(next_msg) # Handle "exceptional conditions"
for s3 in exceptional:
print >>sys.stderr, 'handling exceptional condition for ',s3.getpeername()
# Stop listening for input on the connection
inputs.remove(s3)
if s3 in outputs:
outputs.remove(s3)
s3.close() #Remove message queue
del message_queues[s3]

client coding

 # -*- coding: utf-8 -*-
"""
Created on Sun Oct 23 14:49:20 2016 @author: fuzzier
""" import sys
import socket messages = ['This is the message.',
'It will be sent',
'in parts.'
]
server_address = ('localhost',1000) # Create a TCP/IP socket,创建两个客户端
socks = [socket.socket(socket.AF_INET,socket.SOCK_STREAM),
socket.socket(socket.AF_INET,socket.SOCK_STREAM)
] # Connect the socket to the port where the server is listening
print >>sys.stderr, 'connecting to %s port %s'%server_address
for s in socks:
s.connect(server_address) for message in messages: # Send messages on both sockets
for s2 in socks:
print >>sys.stderr, '%s:sending "%s"'%(s2.getsockname(),message)
s2.send(message) #Read responses on both sockets
for s3 in socks:
data = s3.recv(1024)
print >>sys.stderr, '%s: received "%s"' % (s3.getsockname(), data.upper())
if not data:
print >>sys.stderr,'closing socket',s3.getsockname()
s3.close()

原理图:

运行结果:

异步select的更多相关文章

  1. linux基础编程:IO模型:阻塞/非阻塞/IO复用 同步/异步 Select/Epoll/AIO(转载)

      IO概念 Linux的内核将所有外部设备都可以看做一个文件来操作.那么我们对与外部设备的操作都可以看做对文件进行操作.我们对一个文件的读写,都通过调用内核提供的系统调用:内核给我们返回一个file ...

  2. python 异步 select pooll epoll

    概念: 首先列一下,sellect.poll.epoll三者的区别 select select最早于1983年出现在4.2BSD中,它通过一个select()系统调用来监视多个文件描述符的数组,当se ...

  3. 第十天 多进程、协程(multiprocessing、greenlet、gevent、gevent.monkey、select、selector)

    1.多进程实现方式(类似于多线程) import multiprocessing import time,threading def thread_run():#定义一个线程函数 print(&quo ...

  4. IO模型与select,poll,epoll

    五种:阻塞,非阻塞,IO复印,信号驱动,异步. select,poll,epoll select: 典型用32个32位的整数表示1024个描述符,并发的局限. poll:功能同上,但数据结构不一样(链 ...

  5. 基础10 多进程、协程(multiprocessing、greenlet、gevent、gevent.monkey、select、selector)

    1.多进程实现方式(类似于多线程) import multiprocessing import time,threading def thread_run():#定义一个线程函数 print(&quo ...

  6. 🍛 餐厅吃饭版理解 IO 模型:阻塞 / 非阻塞 / IO 复用 / 信号驱动 / 异步

    IO 概念 一个基本的 IO,它会涉及到两个系统对象,一个是调用这个 IO 的进程对象,另一个就是系统内核 (kernel).当一个 read 操作发生时,它会经历两个阶段: 通过 read 系统调用 ...

  7. python 开发一个支持多用户在线的FTP

    ### 作者介绍:* author:lzl### 博客地址:* http://www.cnblogs.com/lianzhilei/p/5813986.html### 功能实现 作业:开发一个支持多用 ...

  8. [NewLife.XCode]高级查询(化繁为简、分页提升性能)

    NewLife.XCode是一个有10多年历史的开源数据中间件,支持nfx/netcore,由新生命团队(2002~2019)开发完成并维护至今,以下简称XCode. 整个系列教程会大量结合示例代码和 ...

  9. 常见网络编程面试题答案征集与面试题(收集) ZZ 【网络编程】

    http://www.cnblogs.com/wickedboy237/archive/2013/05/12/3074362.html 1:tcp和udp的区别2:流量控制和拥塞控制的实现机制3:滑动 ...

随机推荐

  1. [LeetCode] Nim Game 尼姆游戏

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  2. Activity去Title的几种方式

    第一种:直接加一行代码: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInst ...

  3. 给空签名包进行签名以及找不到keystore证书链问题的解决方案

    转 http://blog.csdn.net/u011106842/article/details/49683865

  4. MySQL基础

    数据库操作 ---终端使用数据库 mysql -u root -p 之后回车键 输入密码 ---显示所有数据库: show databases; ---默认数据库: mysql - 用户权限相关数据 ...

  5. Beta版本冲刺第五天

    Aruba 408 409 410 428 429 431 完成任务: 数据库对于分类新建/删除的更新 调整图片再编辑界面的合适大小 调整常驻通知栏按钮的跳转逻辑 微调数据库 立会照片: 燃尽图: c ...

  6. bzoj4443[SCOI2015]小凸玩矩阵

    题意:一个n*m的矩阵(n<=m<=250),要求选出n个数(每行,每列最多选一个),求第k大数的最小值. 首先第k大的意思是从大到小的第k个数(我读错了,WA了一次还以为算法不对...) ...

  7. Beta阶段项目展示

    1.团队简介 韩青长 前端工程师 我是韩青长,技术小白,抱着对软工的好奇和对未来工作的憧憬选了这门课.暂时选择了测试的工作,也对开发和UI有一定兴趣.从前上帝创造了我们,现在轮到我们来创造自己的软件了 ...

  8. PHP日志记录规范PSR-3

    .note-content { font-family: "Helvetica Neue", Arial, "Hiragino Sans GB", STHeit ...

  9. IOS-Hybrid(混合开发)

    http://www.cnblogs.com/oc-bowen/p/5423902.html 1.1.     APP三种开发模式 智能手机之普及不用多说,手机APP渗投到各个行业:电商(淘宝.京东等 ...

  10. widows和Linux java加密注意事项

    /** * @Title: EncrypAES.java * @Package com.weidinghuo.payment.util * @Description: TODO(用一句话描述该文件做什 ...