Scrapy, a fast high-level web crawling & scraping framework for Python.
Drive benchmark crawls with loop.run_until_complete() (#8234)
Adrian7a6257267586
diff --git a/tests/benchmarks/__init__.py b/tests/benchmarks/__init__.pyindex 82560515a..bfd2df09e 100644--- a/tests/benchmarks/__init__.py+++ b/tests/benchmarks/__init__.py@@ -55,16 +55,12 @@ def crawl(spidercls: type[Spider], settings: dict[str, Any], **kwargs: Any) -> C"""Run a crawl to completion and return its crawler.Unlike the rest of the test suite, benchmarks run without ``pytest-twisted``- and drive the reactor themselves, since the code being measured must be+ and drive the event loop themselves, since the code being measured must becallable synchronously by ``pytest-codspeed``."""from twisted.internet import reactorcrawler = get_crawler(spidercls, settings)- result: list[Any] = []- crawler.crawl(**kwargs).addBoth(result.append)- while not result:- reactor.iterate(0.001)- if isinstance(result[0], BaseException):- raise result[0]+ loop = reactor._asyncioEventloop+ loop.run_until_complete(crawler.crawl(**kwargs).asFuture(loop))return crawlerdiff --git a/tests/benchmarks/conftest.py b/tests/benchmarks/conftest.pyindex 3a5ccd501..86c905ae1 100644--- a/tests/benchmarks/conftest.py+++ b/tests/benchmarks/conftest.py@@ -17,7 +17,7 @@ def running_reactor() -> Generator[None]:from twisted.internet import reactor# Marks the reactor as running without blocking, so that crawls can be- # driven with reactor.iterate(), see tests.benchmarks.crawl().+ # driven with loop.run_until_complete(), see tests.benchmarks.crawl().reactor.startRunning(installSignalHandlers=False)yielddiff --git a/tox.ini b/tox.iniindex 5931e2ffc..37d249312 100644--- a/tox.ini+++ b/tox.ini@@ -394,7 +394,7 @@ commands =# CPU benchmarks, tracked on CodSpeed.## pytest-twisted is left out on purpose: benchmarked code must be callable-# synchronously, so tests/benchmarks drives the reactor itself.+# synchronously, so tests/benchmarks drives the event loop itself.[testenv:benchmark]basepython = python3.14