c-ares是异步DNS请求库,libcurl,libevent,wireshark都使用了c-ares,gevent1.0版本前使用的是libevent,
所以它的DNS请求也是使用c-ares,1.0版本后使用cython封装了c-ares。
c-ares官方文档,http://c-ares.haxx.se/docs.html。
gevent中DNS默认使用的是线程池版本的,可通过设置GEVENT_RESOLVER=ares环境变量使用c-ares异步库。
如何证明的确是异步呢,试着跑一遍你就知道了?
#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_clean