有人使用过Crawler4j吗?
我按照the project page的例子实现了我自己的爬虫。爬虫运行良好,爬行速度非常快。唯一的问题是我总是有20-30秒的延迟。有没有办法避免等待时间?
发布于 2014-05-03 01:01:19
刚查过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)将立即返回。
https://stackoverflow.com/questions/22355130
复制相似问题