使用场景:检测80w URL 可否打开
配置:高端配置,20+进程 500+CONCURRENT_REQUESTS
运行一段时间后会有DNSLookup什么的错误,也就是查找超时,但是在浏览器里可以打开这个网页
首先做一些可能的无用功,爬虫配置 : ‘COOKIES_ENABLED’: False,
‘RETRY_ENABLED’: False,‘DNS_TIMEOUT’: 60,
再配置可能的有用功 : ‘REACTOR_THREADPOOL_MAXSIZE’: 50,
配置完后发现,还是会有很多DNS超时,依然可以在浏览器里打开.
解决方案思路: 1 替换底层getHostByName函数,自己实现系统DNS客户端, 2 替换判断超时代码, 3 将底层异常传递到上层.
过程:
安装dnspython库
将 twisted.internet.base中 getHostByName 函数替换如下
def getHostByName(self, name, timeout=(1, 3, 11, 45)):
"""
See L{twisted.internet.interfaces.IResolverSimple.getHostByName}.
Note that the elements of C{timeout} are summed and the result is used
as a timeout for the lookup. Any intermediate timeout or retry logic
is left up to the platform via L{socket.gethostbyname}.
"""
def resolve_hostname(hostname):
"Print an A or AAAA record for `hostname`; follow CNAMEs if necessa