Scrapy, a fast high-level web crawling & scraping framework for Python.
Deprecate running Crawler.crawl() twice.
Andrey Rakhmatullind3b5c9be9768
diff --git a/docs/topics/api.rst b/docs/topics/api.rstindex 16c28405c..175c877de 100644--- a/docs/topics/api.rst+++ b/docs/topics/api.rst@@ -100,7 +100,7 @@ how you :ref:`configure the downloader middlewaresStarts the crawler by instantiating its spider class with the given``args`` and ``kwargs`` arguments, while setting the execution engine in- motion.+ motion. Should be called only once.Returns a deferred that is fired when the crawl is finished.diff --git a/scrapy/crawler.py b/scrapy/crawler.pyindex 25823b6ac..3c9f28a00 100644--- a/scrapy/crawler.py+++ b/scrapy/crawler.py@@ -124,6 +124,7 @@ class Crawler:self.settings.freeze()self.crawling: bool = False+ self._started: bool = Falseself.spider: Optional[Spider] = Noneself.engine: Optional[ExecutionEngine] = None@@ -131,7 +132,13 @@ class Crawler:def crawl(self, *args: Any, **kwargs: Any) -> Generator[Deferred, Any, None]:if self.crawling:raise RuntimeError("Crawling already taking place")- self.crawling = True+ if self._started:+ warnings.warn(+ "Running Crawler.crawl() more than once is deprecated.",+ ScrapyDeprecationWarning,+ stacklevel=2,+ )+ self.crawling = self._started = Truetry:self.spider = self._create_spider(*args, **kwargs)diff --git a/tests/test_crawl.py b/tests/test_crawl.pyindex ca9084294..96d43b2b9 100644--- a/tests/test_crawl.py+++ b/tests/test_crawl.py@@ -123,7 +123,9 @@ class CrawlTestCase(TestCase):self.assertTrue(crawler.spider.t2 == 0)self.assertTrue(crawler.spider.t2_err > 0)self.assertTrue(crawler.spider.t2_err > crawler.spider.t1)+# server hangs after receiving response headers+ crawler = get_crawler(DelaySpider, {"DOWNLOAD_TIMEOUT": 0.35})yield crawler.crawl(n=0.5, b=1, mockserver=self.mockserver)self.assertTrue(crawler.spider.t1 > 0)self.assertTrue(crawler.spider.t2 == 0)@@ -201,6 +203,7 @@ class CrawlTestCase(TestCase):)self.assertEqual(crawler.spider.visited, 6)+ crawler = get_crawler(DuplicateStartRequestsSpider, settings)yield crawler.crawl(dont_filter=False,distinct_urls=3,diff --git a/tests/test_downloader_handlers.py b/tests/test_downloader_handlers.pyindex 8459408ff..57211d97a 100644--- a/tests/test_downloader_handlers.py+++ b/tests/test_downloader_handlers.py@@ -743,6 +743,7 @@ class Http11MockServerTestCase(unittest.TestCase):# See issue https://twistedmatrix.com/trac/ticket/8175raise unittest.SkipTest("xpayload fails on PY3")+ crawler = get_crawler(SingleRequestSpider, self.settings_dict)request.headers.setdefault(b"Accept-Encoding", b"gzip,deflate")request = request.replace(url=self.mockserver.url("/xpayload"))yield crawler.crawl(seed=request)