epoll.register(fd, EPOLLFLAGS) try: while keep_going: try: results = epoll.poll(config.EPOLL_TIMEOUT) except IOError as e: if e.errno == errno.EINTR and not keep_going: break raise flags = dict(results) ts = time.time() for fd, bot in fds.items(): cflags = flags.get(fd, 0) if cflags & select.EPOLLHUP == select.EPOLLHUP: bot.disconnect() # after a while, check_disconnect will return True elif cflags & select.EPOLLIN == select.EPOLLIN: bot.handle() elif bot.check_disconnect(ts): del fds[fd] try: epoll.unregister(fd) except FileNotFoundError: pass fd = bot.connect() fds[fd] = bot epoll.register(fd, EPOLLFLAGS) commands.whelp(fds.values()) log.flush() for b in fds.values(): b.disconnect()