我们从Python开源项目中,提取了以下16个代码示例,用于说明如何使用select.EPOLLERR。
def run(self): '''???? ??epoll???????? ''' while True: # epoll?????????????????????????????????????????? # ?????????epoll??????? epoll_list = self.epoll_sock.poll() for fd, events in epoll_list: logs.dblog("epoll: epoll find fd(%s) have signal" % fd) sock_state = self.conn_state[fd] # ?? epoll?????io?? epoll hang?????? if select.EPOLLHUP & events: sock_state.state = "closing" # ??IO??epoll????????? elif select.EPOLLERR & events: sock_state.state = "closing" logs.dblog("epoll: use state_machine process fd(%s)" % fd) self.state_machine(fd)
def run(self): '''???? ??epoll???????? ''' while True: # epoll?????????????????????????????????????????? # ?????????epoll??????? epoll_list = self.epoll_sock.poll() for fd, events in epoll_list: sock_state = self.conn_state[fd] # ?? epoll?????io?? epoll hang?????? if select.EPOLLHUP & events: sock_state.state = "closing" # ??IO??epoll????????? elif select.EPOLLERR & events: sock_state.state = "closing" self.state_machine(fd) #}}} #{{{fork_processes
def register(self, sock, events, callback, *args, **kwargs): ev = select.EPOLLERR | select.EPOLLHUP need_modify = False if sock in self.rd_socks: ev |= select.EPOLLIN need_modify = True if sock in self.wr_socks: ev |= select.EPOLLOUT need_modify = True if events & EV_READ: ev |= select.EPOLLIN if events & EV_WRITE: ev |= select.EPOLLOUT if need_modify: self.epoll.modify(sock.fileno(), ev) else: try: self.epoll.register(sock.fileno(), ev) except IOError: return False else: self.fd2socks[sock.fileno()] = sock super(Epoll, self).register(sock, events, callback, *args, **kwargs) return True
def unregister(self, sock, events=EV_READ | EV_WRITE): super(Epoll, self).unregister(sock, events) if events == EV_READ | EV_WRITE: self.epoll.unregister(sock) ck = self.fd2socks.pop(sock.fileno(), None) if ck: return True else: return False else: ev = select.EPOLLERR | select.EPOLLHUP | \ select.EPOLLIN | select.EPOLLOUT if events & EV_READ: ev ^= select.EPOLLIN if events & EV_WRITE: ev ^= select.EPOLLOUT self.epoll.modify(sock.fileno(), ev) return True
def run(self): while True: try: self.check_timer() events = self.epoll.poll(self.MIN_INTERVAL) for fd, event in events: sock = self.fd2socks.get(fd) if not sock: continue if event & select.EPOLLERR or event & select.EPOLLHUP: if self.err_callback: self.err_callback[0](sock, *self.err_callback[1], **self.err_callback[2]) elif event & select.EPOLLIN: callback, args, kwargs = self.rd_socks.get(sock) if callback: callback(sock, *args, **kwargs) elif event & select.EPOLLOUT: callback, args, kwargs = self.wr_socks.get(sock) if callback: callback(sock, *args, **kwargs) except Exception as e: print("exception, %s\n%s" % (e, traceback.format_exc()))
def run(self): while True: dbgPrint("\n -- run func loop") for i in self.conn_state.iterkeys(): dbgPrint("\n -- state of fd: %d" % i) self.conn_state[i].printState(); epoll_list = self.epoll_sock.poll() for fd, events in epoll_list: dbgPrint("\n-- run epoll return fd: %d, event: %s" %(fd, events)) sock_state = self.conn_state[fd] if select.EPOLLHUP & events: dbgPrint("events EPOLLHUP") sock_state.state = "closing" elif select.EPOLLERR & events: dbgPrint("EPOLLERROR") sock_state.state = "closing" self.state_machine(fd)
def run(self): '''???? ??epoll???????? ''' while True: #dbgPrint("\n -- run func loop") #for i in self.conn_state.iterkeys(): # dbgPrint("\n -- state of fd: %d" % i) # self.conn_state[i].printState(); # epoll?? # ???????????????????????????????? # ?????????epoll??????? # epoll???IO epoll_list = self.epoll_sock.poll() for fd, events in epoll_list: dbgPrint("\n-- run epoll return fd: %d, event: %s" %(fd, events)) sock_state = self.conn_state[fd] # ?? epoll?? # ???io???epoll hang?????? if select.EPOLLHUP & events: dbgPrint("events EPOLLHUP") sock_state.state = "closing" # ??IO??epoll????????? elif select.EPOLLERR & events: dbgPrint("EPOLLERROR") sock_state.state = "closing" self.state_machine(fd)
def __convert_epoll_events(self, events): """ Convert epoll events to standard events """ std_events = [] for fileno, event in events: is_read = (event & select.EPOLLIN) == select.EPOLLIN is_write = (event & select.EPOLLOUT) == select.EPOLLOUT is_hup = (event & select.EPOLLHUP) == select.EPOLLHUP is_err = (event & select.EPOLLERR) == select.EPOLLERR std_event = 0 if is_read: std_event |= EV_TYPE_READ if is_write: std_event |= EV_TYPE_WRITE if is_hup: std_event |= EV_TYPE_HUP if is_err: std_event |= EV_TYPE_ERR std_events.append( ( fileno, std_event, self.__users_data.get(fileno, None) ) ) return std_events
def poll(self): time.sleep(0.0000000001) # give epoll.modify a chance if not self._connections: time.sleep(1) return for fileno in self._connections: if fileno not in self._servers: if self._connections[fileno].outbuffer: self._epoll.modify(fileno, self._rw) else: self._epoll.modify(fileno, self._ro) for fileno, event in self._epoll.poll(timeout=1): if fileno in self._servers: server = self._servers[fileno] server.handle_connection() else: if event & select.EPOLLIN: try: con = self._connections[fileno] con._in() except Exception as e: # noqa # logger.exception("{}: {}".format(self._name, e)) con.close() continue if event & select.EPOLLOUT: try: con = self._connections[fileno] con._out() except Exception as e: # noqa con.close() continue if event & (select.EPOLLHUP | select.EPOLLERR): try: con = self._connections[fileno] con.close() continue except: pass
def __init__(self): self.timeout_multiplier = 1 if hasattr(select, 'epoll'): self._poller_name = 'epoll' self._poller = select.epoll() _AsyncPoller._Read = select.EPOLLIN | select.EPOLLPRI _AsyncPoller._Write = select.EPOLLOUT _AsyncPoller._Hangup = select.EPOLLHUP _AsyncPoller._Error = select.EPOLLERR _AsyncPoller._Block = -1 elif hasattr(select, 'kqueue'): self._poller_name = 'kqueue' self._poller = _KQueueNotifier() # kqueue filter values are negative numbers so using # them as flags won't work, so define them as necessary _AsyncPoller._Read = 0x01 _AsyncPoller._Write = 0x02 _AsyncPoller._Hangup = 0x04 _AsyncPoller._Error = 0x08 _AsyncPoller._Block = None elif hasattr(select, 'devpoll'): self._poller_name = 'devpoll' self._poller = select.devpoll() _AsyncPoller._Read = select.POLLIN | select.POLLPRI _AsyncPoller._Write = select.POLLOUT _AsyncPoller._Hangup = select.POLLHUP _AsyncPoller._Error = select.POLLERR _AsyncPoller._Block = -1 self.timeout_multiplier = 1000 elif hasattr(select, 'poll'): self._poller_name = 'poll' self._poller = select.poll() _AsyncPoller._Read = select.POLLIN | select.POLLPRI _AsyncPoller._Write = select.POLLOUT _AsyncPoller._Hangup = select.POLLHUP _AsyncPoller._Error = select.POLLERR _AsyncPoller._Block = -1 self.timeout_multiplier = 1000 else: self._poller_name = 'select' self._poller = _SelectNotifier() _AsyncPoller._Read = 0x01 _AsyncPoller._Write = 0x02 _AsyncPoller._Hangup = 0x04 _AsyncPoller._Error = 0x08 _AsyncPoller._Block = None self._fds = {} self._events = {} self._timeouts = [] self.cmd_read, self.cmd_write = _AsyncPoller._cmd_read_write_fds(self) if hasattr(self.cmd_write, 'getsockname'): self.cmd_read = AsyncSocket(self.cmd_read) self.cmd_read._read_fn = lambda: self.cmd_read._rsock.recv(128) self.interrupt = lambda: self.cmd_write.send('I') else: self.interrupt = lambda: os.write(self.cmd_write._fileno, 'I') self.add(self.cmd_read, _AsyncPoller._Read)
def __init__(self): self.timeout_multiplier = 1 if hasattr(select, 'epoll'): self._poller_name = 'epoll' self._poller = select.epoll() _AsyncPoller._Read = select.EPOLLIN | select.EPOLLPRI _AsyncPoller._Write = select.EPOLLOUT _AsyncPoller._Hangup = select.EPOLLHUP _AsyncPoller._Error = select.EPOLLERR _AsyncPoller._Block = -1 elif hasattr(select, 'kqueue'): self._poller_name = 'kqueue' self._poller = _KQueueNotifier() # kqueue filter values are negative numbers so using # them as flags won't work, so define them as necessary _AsyncPoller._Read = 0x01 _AsyncPoller._Write = 0x02 _AsyncPoller._Hangup = 0x04 _AsyncPoller._Error = 0x08 _AsyncPoller._Block = None elif hasattr(select, 'devpoll'): self._poller_name = 'devpoll' self._poller = select.devpoll() _AsyncPoller._Read = select.POLLIN | select.POLLPRI _AsyncPoller._Write = select.POLLOUT _AsyncPoller._Hangup = select.POLLHUP _AsyncPoller._Error = select.POLLERR _AsyncPoller._Block = -1 self.timeout_multiplier = 1000 elif hasattr(select, 'poll'): self._poller_name = 'poll' self._poller = select.poll() _AsyncPoller._Read = select.POLLIN | select.POLLPRI _AsyncPoller._Write = select.POLLOUT _AsyncPoller._Hangup = select.POLLHUP _AsyncPoller._Error = select.POLLERR _AsyncPoller._Block = -1 self.timeout_multiplier = 1000 else: self._poller_name = 'select' self._poller = _SelectNotifier() _AsyncPoller._Read = 0x01 _AsyncPoller._Write = 0x02 _AsyncPoller._Hangup = 0x04 _AsyncPoller._Error = 0x08 _AsyncPoller._Block = None self._fds = {} self._events = {} self._timeouts = [] self.cmd_read, self.cmd_write = _AsyncPoller._cmd_read_write_fds(self) if hasattr(self.cmd_write, 'getsockname'): self.cmd_read = AsyncSocket(self.cmd_read) self.cmd_read._read_fn = lambda: self.cmd_read._rsock.recv(128) self.interrupt = lambda: self.cmd_write.send(b'I') else: self.interrupt = lambda: os.write(self.cmd_write._fileno, b'I') self.add(self.cmd_read, _AsyncPoller._Read)
def _processStreams(self): if len(self._closedfds) == 3: return if not self._streamLock.acquire(False): self._streamLock.acquire() self._streamLock.release() return try: if self._stdin.len > 0 and self._stdin.pos == 0: # Polling stdin is redundant if there is nothing to write # turn on only if data is waiting to be pushed self._poller.modify(self._fdin, select.EPOLLOUT) pollres = NoIntrPoll(self._poller.poll, 1) for fd, event in pollres: stream = self._fdMap[fd] if event & select.EPOLLOUT and self._stdin.len > 0: buff = self._stdin.read(BUFFSIZE) written = os.write(fd, buff) stream.pos -= len(buff) - written if stream.pos == stream.len: stream.truncate(0) self._poller.modify(fd, 0) elif event & (select.EPOLLIN | select.EPOLLPRI): data = os.read(fd, BUFFSIZE) oldpos = stream.pos stream.pos = stream.len stream.write(data) stream.pos = oldpos elif event & (select.EPOLLHUP | select.EPOLLERR): self._poller.unregister(fd) self._closedfds.append(fd) # I don't close the fd because the original Popen # will do it. if self.stdin.closed and self._fdin not in self._closedfds: self._poller.unregister(self._fdin) self._closedfds.append(self._fdin) self._proc.stdin.close() finally: self._streamLock.release()
def _epoll_select(self, rlist, wlist, xlist, timeout=None): """epoll-based drop-in replacement for select to overcome select limitation on a maximum filehandle value """ if timeout is None: timeout = -1 eventmasks = defaultdict(int) rfd2obj = defaultdict(list) wfd2obj = defaultdict(list) xfd2obj = defaultdict(list) read_evmask = select.EPOLLIN | select.EPOLLPRI # Just in case def store_evmasks(obj_list, evmask, fd2obj): for obj in obj_list: fileno = _to_fileno(obj) eventmasks[fileno] |= evmask fd2obj[fileno].append(obj) store_evmasks(rlist, read_evmask, rfd2obj) store_evmasks(wlist, select.EPOLLOUT, wfd2obj) store_evmasks(xlist, select.EPOLLERR, xfd2obj) poller = select.epoll() for fileno in eventmasks: poller.register(fileno, eventmasks[fileno]) try: events = poller.poll(timeout) revents = [] wevents = [] xevents = [] for fileno, event in events: if event & read_evmask: revents += rfd2obj.get(fileno, []) if event & select.EPOLLOUT: wevents += wfd2obj.get(fileno, []) if event & select.EPOLLERR: xevents += xfd2obj.get(fileno, []) finally: poller.close() return revents, wevents, xevents