Merge pull request #1147 from Curita/pass-crawlers-to-runner-crawl-calls

Allow passing Crawler instances directly to CrawlerRunner.crawl()
This commit is contained in:
Daniel Graña 2015-04-13 14:23:15 -03:00
commit e85679fa6b
2 changed files with 18 additions and 15 deletions

View File

@ -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 <scrapy.crawler.Crawler>` 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

View File

@ -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)