def monitor(self): prev_snapshot = NetworkSnapshot() snapshot = NetworkSnapshot() last_snapshot = time.time() exceptionCount = 0 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.bind(('', self.port)) sock.listen(0) self.logger.info('Listening on port %d' %(self.port)) readFds = set([sock]) while True: try: timeout = last_snapshot + snapshot.interval - time.time() rs, ws, xs = select.select(readFds, [], [], timeout) exceptionCount = 0 if not rs: # timeout, so we take a snapshot with what we got if snapshot != prev_snapshot: self.logger.info('Taking snapshot with %s' %(str(snapshot))) self.cmap.assign_edge_locations(snapshot.edge_locations) self.cmap.update_memcache() snapshot.memcache_save() last_snapshot = time.time() prev_snapshot.edge_locations = snapshot.edge_locations snapshot.clear() continue for readFd in rs: if readFd == sock: # incoming connection conn, addr = sock.accept() readFds.add(conn) else: self.read_and_close(readFd, snapshot.edge_locations) readFds.remove(readFd) except KeyboardInterrupt: self.logger.info('Exiting...') sock.close() exit() except select.error as e: self.logger.exception(e) # this might happen if we miss a deadline with other operations if timeout < 0: last_snapshot += snapshot.interval exceptionCount += 1 # if we get more than 3 exceptions in a row, we give up if exceptionCount > 3: sock.close() exit()