def resolve_dns_name(name): '''Uses a pool of DNSClients to resolve name to an IP address. Keep a cache. ''' # Is name an IP address? try: socket.inet_pton(socket.AF_INET, name) return name except socket.error: # Not a valid IP address resolve it pass if name in hosts: return hosts[name] with synchronized('__diesel__.dns.' + name): try: ips, tm = cache[name] if time.time() - tm > DNS_CACHE_TIME: del cache[name] cache[name] except KeyError: try: with _pool.connection as conn: ips = conn.resolve(name) except (NotFound, Timeout): raise DNSResolutionError("could not resolve A record for %s" % name) cache[name] = ips, time.time() return random.choice(ips)
def sync_loop(): global sync with synchronized(): sync += 1 sleep(random.random()) sync -= 1 print 'SYNC', sync
def sync_loop(): global sync id = random.random() with synchronized(): sync += 1 sleep(random.random()) sync -= 1 print "SYNC", sync