さて、これは、Celery を使用して何をクロールするかをキューに入れる Django プロジェクトで Scrapy を使用する方法です。実際の回避策は、主にhttp://snippets.scrapy.org/snippets/13/にある joehillen のコードから来ました。
まずはtasks.py
ファイル
from celery import task
@task()
def crawl_domain(domain_pk):
from crawl import domain_crawl
return domain_crawl(domain_pk)
次に、crawl.py
ファイル
from multiprocessing import Process
from scrapy.crawler import CrawlerProcess
from scrapy.conf import settings
from spider import DomainSpider
from models import Domain
class DomainCrawlerScript():
def __init__(self):
self.crawler = CrawlerProcess(settings)
self.crawler.install()
self.crawler.configure()
def _crawl(self, domain_pk):
domain = Domain.objects.get(
pk = domain_pk,
)
urls = []
for page in domain.pages.all():
urls.append(page.url())
self.crawler.crawl(DomainSpider(urls))
self.crawler.start()
self.crawler.stop()
def crawl(self, domain_pk):
p = Process(target=self._crawl, args=[domain_pk])
p.start()
p.join()
crawler = DomainCrawlerScript()
def domain_crawl(domain_pk):
crawler.crawl(domain_pk)
ここでの秘訣は、Twisted フレームワークの「ReactorNotRestartable」問題を回避する「マルチプロセッシング インポート プロセスから」です。したがって、基本的に、Celery タスクは「DomainCrawlerScript」オブジェクトを何度も再利用して Scrapy スパイダーとやり取りする「domain_crawl」関数を呼び出します。(私の例が少し冗長であることは承知していますが、複数のバージョンの Python を使用したセットアップでこれを行ったのは理由があります [私の django Web サーバーは実際には python2.4 を使用しており、私のワーカー サーバーは python2.7 を使用しています])
ここでの私の例では、「DomainSpider」は、URL のリストを取得して「start_urls」として設定する変更された Scrapy Spider です。
お役に立てれば!