码迷,mamicode.com
首页 > 编程语言 > 详细

python的epoll及EPOLLLT

时间:2016-10-10 16:27:42      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

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

#! /usr/python

import socket,sys,select

c=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host = 127.0.0.1
port=57777
c.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/python

import socket,sys,select

c=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host = 127.0.0.1
port=57777
c.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,sys

host=127.0.0.1
port=57777

s = 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)

 

python的epoll及EPOLLLT

标签:

原文地址:http://www.cnblogs.com/lijinping/p/5946190.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!