__init__.py 888 B

12345678910111213141516171819202122232425
  1. from concurrent.futures import ThreadPoolExecutor, wait
  2. from crawler.spiders import SearchEngine, SearchDomain
  3. from crawler.utils import err_details
  4. class BreadthCrawler(SearchEngine, SearchDomain):
  5. def __init__(self, workers=1, **kwargs):
  6. SearchEngine.__init__(self, **kwargs)
  7. SearchDomain.__init__(self, **kwargs)
  8. self._workers = workers
  9. def start(self):
  10. with ThreadPoolExecutor(max_workers=self._workers) as executor:
  11. futures = []
  12. f = executor.submit(self.search_engines)
  13. f.add_done_callback(err_details)
  14. futures.append(f)
  15. for _ in range(1, self._workers + 1):
  16. future = executor.submit(self.crawl_spider)
  17. future.add_done_callback(err_details)
  18. futures.append(future)
  19. wait(futures)
  20. print('寻源任务结束')