玩蛇网提供最新Python编程技术信息以及Python资源下载!

请问epoll不触发hup是什么问题

一个简单的例子,当我telnet 127.0.0.1 1234后,我关掉打开telnet的那个终端窗口,为什么不会触发event & select.EPOLLHUP。求解。

import socket
import select
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
server.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
server.bind(('0.0.0.0',1234))
server.listen(10)
server.setblocking(0)

epoll=select.epoll()
epoll.register(server.fileno(),select.EPOLLIN)
print 'server filno:',server.fileno()
try:
    connections={};requests={};responses={}
    while True:
        events=epoll.poll(1)
        for fileno,event in events:
            print 'event', event
            if fileno == server.fileno():
                connection,addr=server.accept()
                connFd=connection.fileno()
                connection.setblocking(0)
                epoll.register(connFd,select.EPOLLIN)
                connections[connFd]=connection
            elif event & select.EPOLLIN:
                requests[fileno]=connections[fileno].recv(1024).strip()
                epoll.modify(fileno,select.EPOLLOUT)

            elif event & select.EPOLLOUT:
                connections[fileno].send(requests[fileno]+'\n')
                epoll.modify(fileno,select.EPOLLIN)

            elif event & select.EPOLLHUP:
                print('close')
                epoll.unregister(fileno)
                connections[fileno].close()
                del connections[fileno]

finally:
    pass
            

当对端正常关闭连接时,会同时触发EPOLLIN和EPOLLHUP:

EPOLLIN:0b1
EPOLLOUT:0b100
EPOLLHUP:0b10000

我稍微改动了你的代码打印了event的二进制值,发现当对端关闭连接时,event的二进制值是:event 0b11001,即 EPOLLIN | EPOLLHUP | 1000,这其实与预期是符合的,EPOLLIN和EPOLLHUP确实是同时触发了,但你的处理代码里用的是elif这样的组合,如果两个分支同时满足,是会被跳过的,恰好你的EPOLLIN写在EPOLLHUP前,所以EPOLLHUP完全被忽略掉了。

玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/wenda/wd18852.html

相关文章 Recommend

玩蛇网Python互助QQ群,欢迎加入-->: 106381465 玩蛇网Python新手群
修订日期:2017年05月12日 - 19时23分50秒 发布自玩蛇网

您现在的位置: 玩蛇网首页 > Python问题解答 > 正文内容
我要分享到:

必知PYTHON教程 Must Know PYTHON Tutorials

必知PYTHON模块 Must Know PYTHON Modules