【Pyhton Network】使用poll()或select()实现非阻塞传输
2014-03-11 10:21
459 查看
通常情况下,socket上的I/O会阻塞。即除非操作结束,否则程序不会照常进行。而以下集中情况需要在非阻塞模式下进行:
1. 网络接口在等待数据时是活动的,可以做出相应;
2. 在不使用线程或进程的情况下也可以同时处理多个网络相关任务;
3. 在网络上等待的时候可以执行其它计算
在以上情况中,可以使用两个标准工具解决,poll和select。它们都可以通知操作系统哪个socket对程序感兴趣,当该socket上有事件发生时,操作系统才调用处理程序。
服务器程序每隔5秒向连接对象发送系统当前时间,如下:
View Code
运行截图如下:
1. 网络接口在等待数据时是活动的,可以做出相应;
2. 在不使用线程或进程的情况下也可以同时处理多个网络相关任务;
3. 在网络上等待的时候可以执行其它计算
在以上情况中,可以使用两个标准工具解决,poll和select。它们都可以通知操作系统哪个socket对程序感兴趣,当该socket上有事件发生时,操作系统才调用处理程序。
服务器程序每隔5秒向连接对象发送系统当前时间,如下:
#! /usr/bin/env python # Nonblocking I/O with select() - Chapter 5 - selectclient.py import socket, sys, select host = 'localhost' port = 51423 spinsize = 10 spinpos = 0 spindir = -1 def spin(): global spinsize, spinpos, spindir spinstr = '.'*spinpos + '|' + '.'*(spinsize-spinpos-1) sys.stdout.write('\r'+spinstr+' ') sys.stdout.flush() spinpos += spindir if spinpos < 0: spinpos = 1 spindir = 1 elif spinpos >= spinsize: spinpos -= 2 spindir = -1 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.connect((host, port)) while 1: infds, outfds, errfds = select.select([s], [], [s], 0.05) # [s] just the same as[s.fileno()], only need to be a list. if len(infds): # Normally, one would use something like "for fd in infds" here. # We don't bother since there will only ever be a single file # descriptor here. data = s.recv(4096) if not len(data): print "\rRemote and closed connection; exiting." break # Only one item in here -- if there's anything, it's for us. sys.stdout.write("\rReceived: "+data) sys.stdout.flush if len(errfds): print "\rProblem occurred; exiting." sys.exit(0) spin()
View Code
运行截图如下:
相关文章推荐
- Linux非阻塞IO(六)使用poll实现非阻塞的服务器端
- Socket 使用select()非阻塞方式实现
- Linux非阻塞IO(五)使用poll实现非阻塞的回射服务器客户端
- 使用select实现非阻塞socket | dbafree首页
- Linux驱动学习8(非阻塞IO的实现--select/poll方法)
- Linux非阻塞IO(五)使用poll实现非阻塞的回射服务器客户端
- Linux非阻塞IO(六)使用poll实现非阻塞的服务器端
- linux 下阻塞睡眠等待poll函数简单实现
- select,poll和epoll使用
- vue 不使用select实现下拉框功能(推荐)
- linux下使用select实现精确定时器
- 使用python2实现http multipart/form-data数据传输
- 事件触发机制:Poll,Select和Epoll实现原理分析
- select(poll)实现代码实现分析(基于kernel 3.10.0)
- select(poll)系统调用实现解析(一)
- 通过CuteFTP用VBScript使用SFTP,实现Win与Linux的文件传输
- epoll 的实现原理以及与poll,select 的对比
- IO-同步异步,阻塞非阻塞,select, poll , epoll
- JavaMail实现收发邮件(五)使用SSL实现加密传输
- 使用样式实现select下拉框选择\jQuery返回顶部top