博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python的epoll及EPOLLLT
阅读量:6873 次
发布时间:2019-06-26

本文共 2841 字,大约阅读时间需要 9 分钟。

今天没事练习python的epoll,一开始写了个客户端:

#! /usr/pythonimport socket,sys,selectc=socket.socket(socket.AF_INET,socket.SOCK_STREAM)host = '127.0.0.1'port=57777c.connect((host,port))epoll_fd = select.epoll()epoll_fd.register(c.fileno(),select.EPOLLIN)epoll_fd.register(sys.stdin.fileno(), select.EPOLLIN)str=""while True:    e_list = epoll_fd.poll()    for fd,events in e_list:      if fd == c.fileno() and events&select.EPOLLIN:        buf =  c.recv(1024)        if not len(buf):           break        print "ser:",buf      if fd == c.fileno() and events & select.EPOLLOUT:        #print 'send msg to ser.'        c.send(str)        epoll_fd.modify(c.fileno(), select.EPOLLIN)      if fd == sys.stdin.fileno() and events & select.EPOLLIN:        str="ssf"        epoll_fd.modify(c.fileno(), select.EPOLLOUT)c.close()

发现服务端总是进入死循环收信息,甚是迷惑。后来修改了 str="ssf"处,修改为raw_input,发现程序正常运行,恍然醒悟,epoll默认

是LT模式,缓冲里的数据没读走,是每次都会触发的,因此,上面的代码修改epoll_fd.register(sys.stdin.fileno(), select.EPOLLIN)

epoll_fd.register(sys.stdin.fileno(), select.EPOLLIN|select.EPOLLOUT) 也是能正常工作的。

附上最终代码:

client.py

 

#! /usr/pythonimport socket,sys,selectc=socket.socket(socket.AF_INET,socket.SOCK_STREAM)host = '127.0.0.1'port=57777c.connect((host,port))epoll_fd = select.epoll()epoll_fd.register(c.fileno(),select.EPOLLIN)epoll_fd.register(sys.stdin.fileno(), select.EPOLLIN)str=""while True:    e_list = epoll_fd.poll()    for fd,events in e_list:      if fd == c.fileno() and events&select.EPOLLIN:        buf =  c.recv(1024)        if not len(buf):           break        print "ser:",buf      if fd == c.fileno() and events & select.EPOLLOUT:        #print 'send msg to ser.'        c.send(str)        epoll_fd.modify(c.fileno(), select.EPOLLIN)      if fd == sys.stdin.fileno() and events & select.EPOLLIN:        str=raw_input("me: ")        epoll_fd.modify(c.fileno(), select.EPOLLOUT)c.close()

 

简单的server和client相似

import socket, os,select,syshost='127.0.0.1'port=57777s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)s.bind((host,port))s.listen(10)epoll_obj=select.epoll()epoll_obj.register(sys.stdin.fileno(), select.EPOLLIN)print 'wait for connect'cs,caddr=s.accept()epoll_obj.register(cs.fileno(), select.EPOLLIN)str=""while 1:    readylist = epoll_obj.poll()    for fd,event in readylist:        if fd == cs.fileno() and event & select.EPOLLIN:            buf = cs.recv(128)            if not len(buf):                cs.close()                break            print "cli: ", buf        if fd == cs.fileno() and event & select.EPOLLOUT:            cs.send(str)            epoll_obj.modify(cs.fileno(), select.EPOLLIN)        if fd == sys.stdin.fileno() and event&select.EPOLLIN:            str=raw_input("me:")            epoll_obj.modify(cs.fileno(), select.EPOLLOUT)

 

转载于:https://www.cnblogs.com/lijinping/p/5946190.html

你可能感兴趣的文章
请保护我们的地球
查看>>
翻转字符串
查看>>
Ext.MessageBox消息框
查看>>
电脑知识:修电脑(转)
查看>>
jQuery 1.7.2 animate功能跨浏览器Bug修补
查看>>
HTML <map>标签的使用
查看>>
Android之dialog
查看>>
freebsd用法汇总[zz]
查看>>
tomcat 默认路径 和 默认起始页的设置
查看>>
去掉 Constraints
查看>>
8天学通MongoDB——第七天 运维技术
查看>>
How Do Annotations Work in Java?--转
查看>>
查看centos中的用户和用户组
查看>>
web.xml中常用元素的解读
查看>>
Direct-X学习笔记--纹理映射
查看>>
使用接口实现多继承
查看>>
Foreach循环输出索引值
查看>>
041 SparkSql的回顾与复习
查看>>
myql基准测试工具Sysbench
查看>>
想拥有一款钢铁侠Jarvis管家的软件吗?
查看>>