我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用asyncore.socket_map()。
def __init__(self, localaddr, remoteaddr): self._localaddr = localaddr self._remoteaddr = remoteaddr asyncore.dispatcher.__init__(self) try: self.create_socket(socket.AF_INET, socket.SOCK_STREAM) # try to re-use a server port if possible self.set_reuse_addr() self.bind(localaddr) self.listen(5) except: # cleanup asyncore.socket_map before raising self.close() raise else: print >> DEBUGSTREAM, \ '%s started at %s\n\tLocal addr: %s\n\tRemote addr:%s' % ( self.__class__.__name__, time.ctime(time.time()), localaddr, remoteaddr)
def maintern_zombie_channel (now, map = None): global _killed_zombies if map is None: map = asyncore.socket_map for channel in list(map.values()): if hasattr (channel, "handle_timeout"): try: # +3 is make gap between server & client iszombie = (now - channel.event_time) > channel.zombie_timeout except AttributeError: continue if iszombie: _killed_zombies += 1 try: channel.handle_timeout () except: channel.handle_error ()
def lifetime_loop (timeout = 30.0, count = 0): global _last_maintern global _maintern_interval loop = 0 map = asyncore.socket_map while map and _shutdown_phase == 0: poll_fun_wrap (timeout, map) now = time.time() if (now - _last_maintern) > _maintern_interval: maintern (now) _last_maintern = time.time () loop += 1 if count and loop > count: break
def graceful_shutdown_loop (): global _shutdown_phase timestamp = time.time() timeout = 1.0 map = asyncore.socket_map while map and _shutdown_phase < 4: time_in_this_phase = time.time() - timestamp veto = 0 for fd,obj in list(map.items()): try: fn = getattr (obj,'clean_shutdown_control') except AttributeError: pass else: try: veto = veto or fn (_shutdown_phase, time_in_this_phase) except: obj.handle_error() if veto and time_in_this_phase < _shutdown_timeout: poll_fun_wrap (timeout, map) else: _shutdown_phase += 1 timestamp = time.time()
def closeall_check(self, usedefault): # Check that close_all() closes everything in a given map l = [] testmap = {} for i in range(10): c = dummychannel() l.append(c) self.assertEqual(c.socket.closed, False) testmap[i] = c if usedefault: socketmap = asyncore.socket_map try: asyncore.socket_map = testmap asyncore.close_all() finally: testmap, asyncore.socket_map = asyncore.socket_map, socketmap else: asyncore.close_all(testmap) self.assertEqual(len(testmap), 0) for c in l: self.assertEqual(c.socket.closed, True)
def poll_fun_wrap (timeout, map = None): global _logger if map is None: map = asyncore.socket_map if EXHAUST_DNS: asyndns.pop_all () try: poll_fun (timeout, map) except (TypeError, OSError) as why: # WSAENOTSOCK remove_notsocks (map) except ValueError: # negative file descriptor, testing all sockets killed = remove_notsocks (map) # or too many file descriptors in select(), divide and conquer if not killed: half = int (len (map) / 2) tmap = {} cc = 0 for k, v in list(map.items ()): tmap [k] = v cc += 1 if cc == half: poll_fun_wrap (timeout, tmap) tmap = {} poll_fun_wrap (timeout, tmap) except: _logger and _logger.trace () raise
def pop_all (self): # DNS query maybe not allowed delay between request and send # maybe they just drop response packet for delaying with self.lock: queue, self.queue = self.queue [:], [] count = len (queue) while queue: name, args = queue.pop (0) self.handler.handle_request (name, **args) if (not count and not self.has_job ()): return map = {} with self.lock: for client in self.udps: map [client._fileno] = client fds = list (map.keys ()) # maybe 2 is enough safeguard = count * 2 while self.has_job () and safeguard: safeguard -= 1 asyncore.loop (0.1, map, count = 1) if safeguard % 5 == 0: self.maintern (time.time ()) self.maintern (time.time ()) for fd in fds: if fd not in map: # resync try: del asyncore.socket_map [fd] except KeyError: pass
def run(self): self.active = True self.__flag.set() while self.active and asyncore.socket_map: self.active_lock.acquire() asyncore.loop(timeout=0.1, count=1) self.active_lock.release() asyncore.close_all(ignore_all=True)
def debugging_server(serv, serv_evt, client_evt): serv_evt.set() try: if hasattr(select, 'poll'): poll_fun = asyncore.poll2 else: poll_fun = asyncore.poll n = 1000 while asyncore.socket_map and n > 0: poll_fun(0.01, asyncore.socket_map) # when the client conversation is finished, it will # set client_evt, and it's then ok to kill the server if client_evt.is_set(): serv.close() break n -= 1 except socket.timeout: pass finally: if not client_evt.is_set(): # allow some time for the client to read the result time.sleep(0.5) serv.close() asyncore.close_all() serv_evt.set()
def loop_waiting_for_flag(self, instance, timeout=5): timeout = float(timeout) / 100 count = 100 while asyncore.socket_map and count > 0: asyncore.loop(timeout=0.01, count=1, use_poll=self.use_poll) if instance.flag: return count -= 1 time.sleep(timeout) self.fail("flag not set")
def setSocketMap(self, sockMap=socket_map): self.__sockMap = sockMap
def run(self): self._active = True self.__flag.set() while self._active and asyncore.socket_map: self._active_lock.acquire() asyncore.loop(timeout=0.001, count=1) self._active_lock.release() asyncore.close_all()