diff --git a/docs/topics/api.rst b/docs/topics/api.rst index 57b8ee0cf..2055682dc 100644 --- a/docs/topics/api.rst +++ b/docs/topics/api.rst @@ -101,7 +101,7 @@ how you :ref:`configure the downloader middlewares .. class:: CrawlerRunner(settings) - This is a convenient helper class that creates, configures and runs + This is a convenient helper class that keeps track of, manages and runs crawlers inside an already setup Twisted `reactor`_. The CrawlerRunner object must be instantiated with a @@ -116,23 +116,23 @@ how you :ref:`configure the downloader middlewares Set of :class:`crawlers ` created by the :meth:`crawl` method. - .. attribute:: crawl_deferreds + .. method:: crawl(crawler_or_spidercls, \*args, \**kwargs) - Set of the `deferreds`_ return by the :meth:`crawl` method. This - collection it's useful for keeping track of current crawling state. + This method runs a crawler with the provided arguments. - .. method:: crawl(spidercls, \*args, \**kwargs) + It will keep track of the given crawler so it can be stopped later, + while calling its :meth:`Crawler.crawl` method. - This method sets up the crawling of the given `spidercls` with the - provided arguments. - - It takes care of loading the spider class while configuring and starting - a crawler for it. + If `crawler_or_spidercls` isn't a :class:`~scrapy.crawler.Crawler` + instance, it will try to create one using this parameter as the spider + class given to it. Returns a deferred that is fired when the crawl is finished. - :param spidercls: spider class or spider's name inside the project - :type spidercls: :class:`~scrapy.spider.Spider` subclass or str + :param crawler_or_spidercls: already created crawler, or a spider class + or spider's name inside the project to create it + :type crawler_or_spidercls: :class:`~scrapy.crawler.Crawler` instance, + :class:`~scrapy.spider.Spider` subclass or string :param args: arguments to initializate the spider :type args: list diff --git a/scrapy/crawler.py b/scrapy/crawler.py index 692a896be..615303528 100644 --- a/scrapy/crawler.py +++ b/scrapy/crawler.py @@ -82,9 +82,12 @@ class CrawlerRunner(object): self.crawlers = set() self._active = set() - def crawl(self, spidercls, *args, **kwargs): - crawler = self._create_crawler(spidercls) - self._setup_crawler_logging(crawler) + def crawl(self, crawler_or_spidercls, *args, **kwargs): + crawler = crawler_or_spidercls + if not isinstance(crawler_or_spidercls, Crawler): + crawler = self._create_crawler(crawler_or_spidercls) + self._setup_crawler_logging(crawler) + self.crawlers.add(crawler) d = crawler.crawl(*args, **kwargs) self._active.add(d)