【问题标题】:signal only works in main thread信号仅在主线程中有效
【发布时间】:2016-03-10 10:43:14
【问题描述】:

我是 django 的新手。我正在尝试通过 django 视图运行我的爬虫蜘蛛。当我通过命令提示符运行时,我的 scrapy 代码运行良好。但是当我尝试在 django 上运行它时它失败了。 错误信息:信号仅在主线程中有效。

我在 django 视图中的代码(如下)

from twisted.internet import reactor
from scrapy.crawler import Crawler
from scrapy.crawler import CrawlerProcess
from scrapy import log, signals
from Working.spiders.workSpider import WorkSpider
from scrapy.settings import Settings
from scrapy.utils.project import get_project_settings

spider = WorkSpider(domain='scrapinghub.com')
crawler = CrawlerProcess(Settings())
crawler.start()
crawler.signals.connect(reactor.stop, signal=signals.spider_closed)
crawler.configure()
crawler.crawl(spider)
crawler.start()
log.start()
reactor.run()

请帮我解决这个问题。谢谢

【问题讨论】:

  • 你能解决它吗?能否请您发布您的工作?
  • 和上面那个人一样。 ????
  • 有同样的问题,但使用 pypetter
  • 有人吗? CrawlerRunner 看起来级别较低,我更喜欢 CrawlerProcess...?
  • reactor.run(0) 将关闭信号

标签: python django scrapy


【解决方案1】:

错误基本上是说你不在主线程中,所以信号没有被处理。

从 CrawlerProcess 切换到 CrawlerRunner 为我解决了这个问题(我猜在 CrawlerRunner 中你在主线程中)http://doc.scrapy.org/en/latest/topics/api.html#scrapy.crawler.CrawlerRunner

希望对你有帮助

【讨论】:

  • 这不是关于您当前的程序在哪个线程中工作吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-06
  • 2019-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多