【发布时间】:2014-03-12 14:47:52
【问题描述】:
有人使用过 Crawler4j 吗?
我按照the project page的例子实现了自己的爬虫。爬虫工作正常,爬得很快。唯一的问题是我总是有 20-30 秒的延迟。有没有办法避免等待时间?
【问题讨论】:
-
您是指处理时间还是等待时间?我知道的唯一与等待相关的设置是“politeness delay”。
标签: java web-crawler crawler4j
有人使用过 Crawler4j 吗?
我按照the project page的例子实现了自己的爬虫。爬虫工作正常,爬得很快。唯一的问题是我总是有 20-30 秒的延迟。有没有办法避免等待时间?
【问题讨论】:
标签: java web-crawler crawler4j
刚刚检查了 crawler4j source code。 CrawerController.start 方法有很多固定的 10 秒“暂停”,以确保线程已完成并准备好被清理。
// Make sure again that none of the threads
// are
// alive.
logger.info("It looks like no thread is working, waiting for 10 seconds to make sure...");
sleep(10);
// ... more code ...
logger.info("No thread is working and no more URLs are in queue waiting for another 10 seconds to make sure...");
sleep(10);
// ... more code ...
logger.info("Waiting for 10 seconds before final clean up...");
sleep(10);
此外,主循环每 10 秒检查一次,以了解爬取线程是否已完成:
while (true) {
sleep(10);
// code to check if some thread is still working
}
protected void sleep(int seconds) {
try {
Thread.sleep(seconds * 1000);
} catch (Exception ignored) {
}
}
因此,微调这些调用并减少睡眠时间可能是值得的。
如果您可以抽出一些时间,更好的解决方案是重写此方法。我会用ExecutorService 替换List<Thread> threads,它的awaitTermination 方法会特别方便。与睡眠不同,awaitTermination(10, TimeUnit.SECONDS) 将在所有任务完成后立即返回。
【讨论】: