def __init__(self, bindaddr, registry, dns_servers=None): DatagramServer.__init__(self, bindaddr) self._registry = registry self._resolver = None if dns_servers: self._resolver = Resolver(servers=dns_servers, timeout=DNS_RESOLVER_TIMEOUT, tries=1)
def __init__(self, bindaddr, registry, dns_servers=None): DatagramServer.__init__(self, bindaddr) self._registry = registry self._resolver = None if dns_servers: log.info("[namesrv] starting with failover resolver %s", dns_servers) self._resolver = Resolver(servers=dns_servers, timeout=DNS_RESOLVER_TIMEOUT, tries=1)
address = ('127.0.0.10', 53) listener = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: listener.bind(address) except socket.error as ex: if 'permission denied' in str(ex).lower(): sys.stderr.write( 'This test binds on port 53 and thus must be run as root.\n') sys.exit(0) raise def reader(): while True: print(listener.recvfrom(10000)) gevent.spawn(reader) r = gevent.get_hub().resolver = Resolver(servers=['127.0.0.10'], timeout=0.001, tries=1) try: result = r.gethostbyname('www.google.com') except socket.gaierror as ex: if 'ARES_ETIMEOUT' not in str(ex): raise else: raise AssertionError('Expected timeout, got %r' % (result, ))
text += ('\n%s failed with %s\n\n' % ( context, getattr(type, '__name__', 'exception'), )) try: from UnityEngine import Debug Debug.LogError(text) except: # f**k pass from gevent.resolver_ares import Resolver hub = gevent.hub.get_hub() hub.resolver = Resolver(hub=hub) class UnityLogStream(object): def write(self, data): from UnityEngine import Debug Debug.Log(data) import logging logging.basicConfig(stream=UnityLogStream()) logging.getLogger().setLevel(logging.ERROR) class ExecutiveWrapper(object): def __init__(self, executive, warpgate):
pool = get_hub().threadpool pool.apply_async(f,(6,33),callback=g) pool.apply_async(f,(2,22),callback=g) gevent.sleep(10) 14.c-ares http://c-ares.haxx.se/docs.html 如何证明的确是异步呢: #coding=utf8 import socket import gevent from gevent import get_hub from gevent.resolver_ares import Resolver r = get_hub().resolver = Resolver(servers=['8.8.8.8']) def f(w): print w,r.gethostbyname(w) for w in ['www.google.com','www.baidu.com','www.apple.com']: gevent.spawn(f,w) gevent.sleep(6) 试着跑一遍你就知道了? cares.ares_library_init(cares.ARES_LIB_INIT_ALL) 初始化ares库,其实只对windows平台做了处理,主要是为了加载iphlpapi.dll,在非windows平台可不调用。 如果调用一定要在c-ares任何函数之前调用。 cares.ares_library_cleanup()
sys.exit(0) from gevent import socket print(gevent.__file__) address = ('', 7153) listener = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: listener.bind(address) except socket.error as ex: if ex.errno in (errno.EPERM, errno.EADDRNOTAVAIL) or 'permission denied' in str(ex).lower(): sys.stderr.write('This test binds on port a port that was already in use or not allowed.\n') sys.exit(0) raise def reader(): while True: print(listener.recvfrom(10000)) gevent.spawn(reader) r = gevent.get_hub().resolver = Resolver(servers=['127.0.0.1'], timeout=0.001, tries=1, udp_port=address[-1]) try: result = r.gethostbyname('www.google.com') except socket.gaierror as ex: if 'ARES_ETIMEOUT' not in str(ex): raise else: raise AssertionError('Expected timeout, got %r' % (result, ))