{"id":228,"date":"2017-10-16T03:45:38","date_gmt":"2017-10-16T03:45:38","guid":{"rendered":""},"modified":"2017-10-16T03:45:38","modified_gmt":"2017-10-16T03:45:38","slug":"","status":"publish","type":"post","link":"http:\/\/weizn.net\/?p=228","title":{"rendered":"Python\u4e2d\u7684epoll\u6a21\u578b"},"content":{"rendered":"<p>\n\t\u53c2\u8003\uff1a<a href=\"https:\/\/harveyqing.gitbooks.io\/python-read-and-write\/content\/python_advance\/how_to_use_linux_epoll.html\" target=\"_blank\">https:\/\/harveyqing.gitbooks.io\/python-read-and-write\/content\/python_advance\/how_to_use_linux_epoll.html<\/a>\n<\/p>\n<p>\n\t\n<\/p>\n<p>\n\t# Demo\n<\/p>\n<p>\n\t#!\/usr\/bin\/env python<br \/>\n# -*- coding: utf-8 -*-<br \/>\n# @Time &nbsp; &nbsp;: 2017\/10\/16 10:56<br \/>\n# @Author &nbsp;: weizinan<br \/>\n# @File &nbsp; &nbsp;: epoll_serv.py<\/p>\n<p>import select<br \/>\nimport socket<br \/>\nimport logging<br \/>\nimport time<\/p>\n<p>class EpollServ(object):<br \/>\n&nbsp; &nbsp; class ClientConnInfo(object):<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; def __init__(self, cliAddr):<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.connTimestamp &nbsp; = time.time()<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.cliAddr &nbsp; &nbsp; &nbsp; &nbsp; = cliAddr<\/p>\n<p>&nbsp; &nbsp; def __init__(self, listenIp, listenPort, recvTimeo=None):<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; super(EpollServ, self).__init__()<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; self.listenIp &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = listenIp<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; self.listenPort &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = listenPort<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; self.recvTimeo &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= recvTimeo<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; self.listenSoc &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;= None<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; self.epollObj &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = None<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; self.cliConnDic &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = dict()<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; self.logger &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; = self._init_logger(&#8220;EpollServ&#8221;, logging.INFO)<\/p>\n<p>&nbsp; &nbsp; ########################################################################################<br \/>\n&nbsp; &nbsp; def _create_listen_socket(self, listenIp, listenPort):<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; try:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listenSoc = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; except Exception, e:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.logger.critical(&#8220;Create listen socket failed. Exception: &#8221; + str(e))<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listenSoc = None<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; else:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listenSoc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) &nbsp;# \u7aef\u53e3\u91ca\u653e\u540e\u53ef\u88ab\u7acb\u5373\u4f7f\u7528<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listenSoc.bind((listenIp, listenPort))<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listenSoc.listen(socket.SOMAXCONN)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listenSoc.setblocking(0) &nbsp;# \u8bbe\u7f6e\u975e\u963b\u585e<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except Exception, e1:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.logger.critical(&#8220;Setting socket failed. Exception: &#8221; + str(e1))<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listenSoc.close()<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; listenSoc = None<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.logger.info(&#8220;Listening socket on %s:%d&#8221; % (listenIp, listenPort))<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; return listenSoc<\/p>\n<p>&nbsp; &nbsp; ########################################################################################<br \/>\n&nbsp; &nbsp; def pkt_handler(self, pkt):<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; print pkt<\/p>\n<p>&nbsp; &nbsp; ########################################################################################<br \/>\n&nbsp; &nbsp; def _recv_from_client(self, cliSoc):<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; if self.cliConnDic.has_key(cliSoc) is False:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; try:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recvBuf = cliSoc.recv(65536)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; except Exception, e:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -2<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; else:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if recvBuf is None:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -3<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recvLen = len(recvBuf)<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; try:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.pkt_handler(recvBuf)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; except Exception, e:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.logger.error(&#8220;pkt_handler error. Exception: &#8221; + str(e))<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; return recvLen<\/p>\n<p>&nbsp; &nbsp; ########################################################################################<br \/>\n&nbsp; &nbsp; def _clean_conn_socket(self, cliSoc):<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; try:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.epollObj.unregister(cliSoc.fileno()) &nbsp;# \u6ce8\u9500socket\u63cf\u8ff0\u5b57<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # cliSoc.shutdown(socket.SHUT_RDWR)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cliSoc.close()<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; except Exception, e:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.logger.error(&#8220;Closed socket failed. Exception: &#8221; + str(e))<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; finally:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; del self.cliConnDic[cliSoc]<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except Exception, e2:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.logger.error(&#8220;Delete client socket from list failed. Exception: &#8221; + str(e2))<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; return<\/p>\n<p>&nbsp; &nbsp; ########################################################################################<br \/>\n&nbsp; &nbsp; def listen_service(self):<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; while True:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.listenSoc is None or self.epollObj is None:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.listenSoc = self._create_listen_socket(self.listenIp, self.listenPort)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.listenSoc is None:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time.sleep(5)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u521b\u5efaepoll\u5bf9\u8c61<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.epollObj = select.epoll()<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except Exception, e:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.logger.error(&#8220;Create epoll object failed. Exception: &#8221; + str(e))<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.listenSoc.close()<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.listenSoc = None<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.epollObj = None<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time.sleep(10)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u6ce8\u518c\u611f\u5174\u8da3\u4e8b\u4ef6<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.epollObj.register(self.listenSoc.fileno(),<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;select.EPOLLIN | select.EPOLLERR | select.EPOLLHUP)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except Exception, e1:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.logger.error(&#8220;Registration epoll event failed. Exception: &#8221; + str(e1))<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.listenSoc.close()<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.listenSoc = None<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.epollObj.close()<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.epollObj = None<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time.sleep(5)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u76d1\u542cepoll\u4e8b\u4ef6<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; epollEventList = self.epollObj.poll(1)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except Exception, e:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.logger.error(&#8220;Wait for epoll events failed. Exception: &#8221; + str(e))<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.epollObj.close()<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.listenSoc.close()<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.epollObj = None<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.listenSoc = None<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; time.sleep(5)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u5904\u7406\u4e8b\u4ef6<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for fdSoc, event in epollEventList:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if fdSoc == self.listenSoc.fileno():<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u76d1\u542csocket\u63cf\u8ff0\u5b57\u76f8\u540c\uff0c\u6709\u65b0\u8fde\u63a5<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cliSoc, cliAddr = self.listenSoc.accept()<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cliSoc.setblocking(0) &nbsp;# \u8bbe\u7f6e\u975e\u963b\u585e<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.epollObj.register(cliSoc.fileno(), select.EPOLLIN | select.EPOLLERR | select.EPOLLHUP)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.cliConnDic[cliSoc] = self.ClientConnInfo(cliAddr)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; except Exception, e:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.logger.error(&#8220;Accepted connection failed. Exception: &#8221; + str(e))<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.logger.info(&#8220;Accepted a new connection from %s:%d&#8221; % (cliAddr[0], cliAddr[1]))<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif event &amp; select.EPOLLIN:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u6709\u6570\u636e\u53ef\u63a5\u6536<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for cliSoc, connInfo in self.cliConnDic.iteritems():<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if cliSoc.fileno() == fdSoc:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u5339\u914d\u4e2d\u89e6\u53d1\u4e8b\u4ef6\u7684socket<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recvLen = self._recv_from_client(cliSoc)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if recvLen &lt;= 0:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.logger.info(&#8220;Disconnected from %s:%d&#8221; %<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(connInfo.cliAddr[0], connInfo.cliAddr[1]))<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.logger.info(&#8220;Closed connection at %s:%d&#8221; %<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(connInfo.cliAddr[0], connInfo.cliAddr[1]))<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self._clean_conn_socket(cliSoc)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # socket\u5f02\u5e38<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for cliSoc, connInfo in self.cliConnDic.iteritems():<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if cliSoc.fileno() == fdSoc:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u5339\u914d\u4e2d\u89e6\u53d1\u4e8b\u4ef6\u7684socket<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.logger.info(&#8220;Connection error at %s:%d.&#8221; %<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (connInfo.cliAddr[0], connInfo.cliAddr[1]))<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.logger.info(&#8220;Closed connection at %s:%d&#8221; %<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(connInfo.cliAddr[0], connInfo.cliAddr[1]))<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self._clean_conn_socket(cliSoc)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # \u5173\u95ed\u8d85\u65f6\u8fde\u63a5<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if self.recvTimeo is not None and self.recvTimeo &gt;= 0:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for cliSoc, connInfo in self.cliConnDic.iteritems():<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if time.time() &#8211; self.cliConnDic[cliSoc].connTimestamp &gt;= self.recvTimeo:<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.logger.info(&#8220;Connection timeout at %s:%d.&#8221; %<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(connInfo.cliAddr[0], connInfo.cliAddr[1]))<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.logger.info(&#8220;Closed connection at %s:%d&#8221; %<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(connInfo.cliAddr[0], connInfo.cliAddr[1]))<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self._clean_conn_socket(cliSoc)<\/p>\n<p>&nbsp; &nbsp; ########################################################################################<br \/>\n&nbsp; &nbsp; def _init_logger(self, loggerName, logLevel):<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; # \u521b\u5efa\u65e5\u5fd7\u8bb0\u5f55<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; logger = logging.getLogger(loggerName)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; logger.setLevel(logLevel)<\/p>\n<p>&nbsp; &nbsp; &nbsp; &nbsp; logFormatter = logging.Formatter(&#8216;[%(asctime)s] [%(levelname)s] -&gt; %(message)s&#8217;)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; consoleHandler = logging.StreamHandler()<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; consoleHandler.setFormatter(logFormatter)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; consoleHandler.setLevel(logLevel)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; logger.addHandler(consoleHandler)<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; return logger<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\n\t\u53c2\u8003\uff1a<a href=\"https:\/\/harveyqing.gitbooks.io\/python-read-and-write\/content\/python_advance\/how_to_use_linux_epoll.html\" target=\"_blank\">https:\/\/harveyqing.gitbooks.io\/python-read-and-write\/content\/python_advance\/how_to_use_linux_epoll.html<\/a>\n<\/p>\n<p>\n\t\n<\/p>\n<p>\n\t# Demo\n<\/p>\n<p>\n\tclass PerimeterIp(threading.Thread):\n<\/p>\n<p>&nbsp; &nbsp; stPeriInfoDic &nbsp; &nbsp; &amp;&#8230;<\/p>\n","protected":false},"author":1,"featured_media":557,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[334],"tags":[],"class_list":["post-228","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python\u4e2d\u7684epoll\u6a21\u578b - Wayne&#039;s Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/weizn.net\/?p=228\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python\u4e2d\u7684epoll\u6a21\u578b - Wayne&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"\u53c2\u8003\uff1ahttps:\/\/harveyqing.gitbooks.io\/python-read-and-write\/content\/python_advance\/how_to_use_linux_epoll.html       # Demo    class PerimeterIp(threading.Thread):  &nbsp; &nbsp; stPeriInfoDic &nbsp; &nbsp; &amp;...\" \/>\n<meta property=\"og:url\" content=\"http:\/\/weizn.net\/?p=228\" \/>\n<meta property=\"og:site_name\" content=\"Wayne&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2017-10-16T03:45:38+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/timg-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1004\" \/>\n\t<meta property=\"og:image:height\" content=\"582\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"zinan\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"http:\/\/weizn.net\/#website\",\"url\":\"http:\/\/weizn.net\/\",\"name\":\"Wayne&#039;s Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"http:\/\/weizn.net\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"ImageObject\",\"@id\":\"http:\/\/weizn.net\/?p=228#primaryimage\",\"inLanguage\":\"zh-Hans\",\"url\":\"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/timg-1.jpg\",\"contentUrl\":\"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/timg-1.jpg\",\"width\":1004,\"height\":582},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/weizn.net\/?p=228#webpage\",\"url\":\"http:\/\/weizn.net\/?p=228\",\"name\":\"Python\\u4e2d\\u7684epoll\\u6a21\\u578b - Wayne&#039;s Blog\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/#website\"},\"primaryImageOfPage\":{\"@id\":\"http:\/\/weizn.net\/?p=228#primaryimage\"},\"datePublished\":\"2017-10-16T03:45:38+00:00\",\"dateModified\":\"2017-10-16T03:45:38+00:00\",\"breadcrumb\":{\"@id\":\"http:\/\/weizn.net\/?p=228#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/weizn.net\/?p=228\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"http:\/\/weizn.net\/?p=228#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\\u9996\\u9875\",\"item\":\"http:\/\/weizn.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python\\u4e2d\\u7684epoll\\u6a21\\u578b\"}]},{\"@type\":\"Article\",\"@id\":\"http:\/\/weizn.net\/?p=228#article\",\"isPartOf\":{\"@id\":\"http:\/\/weizn.net\/?p=228#webpage\"},\"author\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"headline\":\"Python\\u4e2d\\u7684epoll\\u6a21\\u578b\",\"datePublished\":\"2017-10-16T03:45:38+00:00\",\"dateModified\":\"2017-10-16T03:45:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"http:\/\/weizn.net\/?p=228#webpage\"},\"wordCount\":2126,\"commentCount\":0,\"publisher\":{\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\"},\"image\":{\"@id\":\"http:\/\/weizn.net\/?p=228#primaryimage\"},\"thumbnailUrl\":\"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/timg-1.jpg\",\"articleSection\":[\"Python\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"http:\/\/weizn.net\/?p=228#respond\"]}]},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264\",\"name\":\"zinan\",\"logo\":{\"@id\":\"http:\/\/weizn.net\/#personlogo\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python\u4e2d\u7684epoll\u6a21\u578b - Wayne&#039;s Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"http:\/\/weizn.net\/?p=228","og_locale":"zh_CN","og_type":"article","og_title":"Python\u4e2d\u7684epoll\u6a21\u578b - Wayne&#039;s Blog","og_description":"\u53c2\u8003\uff1ahttps:\/\/harveyqing.gitbooks.io\/python-read-and-write\/content\/python_advance\/how_to_use_linux_epoll.html       # Demo    class PerimeterIp(threading.Thread):  &nbsp; &nbsp; stPeriInfoDic &nbsp; &nbsp; &amp;...","og_url":"http:\/\/weizn.net\/?p=228","og_site_name":"Wayne&#039;s Blog","article_published_time":"2017-10-16T03:45:38+00:00","og_image":[{"width":1004,"height":582,"url":"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/timg-1.jpg","path":"\/app\/wp-content\/uploads\/2019\/01\/timg-1.jpg","size":"full","id":557,"alt":"","pixels":584328,"type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"zinan","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"11 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebSite","@id":"http:\/\/weizn.net\/#website","url":"http:\/\/weizn.net\/","name":"Wayne&#039;s Blog","description":"","publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"http:\/\/weizn.net\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"ImageObject","@id":"http:\/\/weizn.net\/?p=228#primaryimage","inLanguage":"zh-Hans","url":"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/timg-1.jpg","contentUrl":"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/timg-1.jpg","width":1004,"height":582},{"@type":"WebPage","@id":"http:\/\/weizn.net\/?p=228#webpage","url":"http:\/\/weizn.net\/?p=228","name":"Python\u4e2d\u7684epoll\u6a21\u578b - Wayne&#039;s Blog","isPartOf":{"@id":"http:\/\/weizn.net\/#website"},"primaryImageOfPage":{"@id":"http:\/\/weizn.net\/?p=228#primaryimage"},"datePublished":"2017-10-16T03:45:38+00:00","dateModified":"2017-10-16T03:45:38+00:00","breadcrumb":{"@id":"http:\/\/weizn.net\/?p=228#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["http:\/\/weizn.net\/?p=228"]}]},{"@type":"BreadcrumbList","@id":"http:\/\/weizn.net\/?p=228#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"http:\/\/weizn.net\/"},{"@type":"ListItem","position":2,"name":"Python\u4e2d\u7684epoll\u6a21\u578b"}]},{"@type":"Article","@id":"http:\/\/weizn.net\/?p=228#article","isPartOf":{"@id":"http:\/\/weizn.net\/?p=228#webpage"},"author":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"headline":"Python\u4e2d\u7684epoll\u6a21\u578b","datePublished":"2017-10-16T03:45:38+00:00","dateModified":"2017-10-16T03:45:38+00:00","mainEntityOfPage":{"@id":"http:\/\/weizn.net\/?p=228#webpage"},"wordCount":2126,"commentCount":0,"publisher":{"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264"},"image":{"@id":"http:\/\/weizn.net\/?p=228#primaryimage"},"thumbnailUrl":"http:\/\/weizn.net\/wp-content\/uploads\/2019\/01\/timg-1.jpg","articleSection":["Python"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["http:\/\/weizn.net\/?p=228#respond"]}]},{"@type":["Person","Organization"],"@id":"http:\/\/weizn.net\/#\/schema\/person\/e88bc12c590502d8b6249326f960b264","name":"zinan","logo":{"@id":"http:\/\/weizn.net\/#personlogo"}}]}},"_links":{"self":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/228","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=228"}],"version-history":[{"count":0,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/posts\/228\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=\/wp\/v2\/media\/557"}],"wp:attachment":[{"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/weizn.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}