V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Feiox
V2EX  ›  Python

V 友救我 ~ 基于 Celery 的爬虫遭遇各种问题

  •  
  •   Feiox ·
    feiox · 2015-05-09 09:12:55 +08:00 · 4676 次点击
    这是一个创建于 3277 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我们目前在基于 Celery 队列做定向爬虫。目标网页很简单,但存储的时候要将目标页上的各种信息转换、分解为结构化数据。在这期间需要查询很多次数据库。
    采用 “发出任务 -> 队列 -> 在 Celery 中执行” 的方式。目前遇到以下问题

    1. 使用 supervisor 管理 Celery 的运行。但发现每隔一段时间,启动的多个 Celery 进程就好一起退出(同时)。

      supervisor.log 如下(部分)

      2015-05-09 03:02:56,462 INFO stopped: celerya (exit status 0)
      2015-05-09 03:02:57,457 INFO stopped: celeryb (exit status 0)
      2015-05-09 03:04:38,275 INFO spawned: 'celerya' with pid 3547
      2015-05-09 03:04:48,529 INFO success: celerya entered RUNNING state, process has stayed up for > than 10 seconds (startsecs)
      2015-05-09 03:07:55,995 INFO spawned: 'celeryb' with pid 3926
      2015-05-09 03:08:06,337 INFO success: celeryb entered RUNNING state, process has stayed up for > than 10 seconds (startsecs)
      2015-05-09 03:09:01,861 INFO stopped: celerya (exit status 0)
      2015-05-09 03:11:29,792 INFO stopped: celeryb (exit status 0)
      2015-05-09 03:12:02,037 INFO spawned: 'celerya' with pid 4706
      2015-05-09 03:12:03,044 INFO spawned: 'celeryb' with pid 4710
      

      退出时 celery 的异常信息如下:

      Traceback (most recent call last):
        File "/usr/local/opt/pyenv/versions/calvino/lib/python2.7/site-packages/celery/worker/__init__.py", line 206, in start
          self.blueprint.start(self)
        File "/usr/local/opt/pyenv/versions/calvino/lib/python2.7/site-packages/celery/bootsteps.py", line 123, in start
          step.start(parent)
        File "/usr/local/opt/pyenv/versions/calvino/lib/python2.7/site-packages/celery/bootsteps.py", line 374, in start
          return self.obj.start()
        File "/usr/local/opt/pyenv/versions/calvino/lib/python2.7/site-packages/celery/worker/consumer.py", line 278, in start
          blueprint.start(self)
        File "/usr/local/opt/pyenv/versions/calvino/lib/python2.7/site-packages/celery/bootsteps.py", line 123, in start
          step.start(parent)
        File "/usr/local/opt/pyenv/versions/calvino/lib/python2.7/site-packages/celery/worker/consumer.py", line 821, in start
          c.loop(*c.loop_args())
        File "/usr/local/opt/pyenv/versions/calvino/lib/python2.7/site-packages/celery/worker/loops.py", line 97, in synloop
          connection.drain_events(timeout=2.0)
        File "/usr/local/opt/pyenv/versions/calvino/lib/python2.7/site-packages/kombu/connection.py", line 275, in drain_events
          return self.transport.drain_events(self.connection, **kwargs)
        File "/usr/local/opt/pyenv/versions/calvino/lib/python2.7/site-packages/kombu/transport/virtual/__init__.py", line 840, in drain_events
          message, queue = item
      TypeError: 'NoneType' object is not iterable
      
    2. 在运行 4 小时之后,celery 进程虽然活着,但却停止工作,无 log,手动重启之后恢复工作。但 80% 的任务出现数据库异常,提示 IntegrityError: (IntegrityError) (1062, u"Duplicate entry '201173000000006801' for key 'PRIMARY'") 错误。手动查找 改主键却不存在。

    3. 在运行 2 小时左右之后,开始出现 陆续出现主键重复错误,手动查找,该数据确实存在。但实际上应该不存在。因为该条数据就是在本次任务中创建的。

    附: celery 运行参数:/usr/local/opt/pyenv/versions/calvino/bin/celery -A celeryd worker -P eventlet -c 30 -n spider02

    刚开始为公司写用于生产环境的代码,救bug 如救火,各位 V 友救我!

    第 1 条附言  ·  2015-05-09 15:17:22 +08:00
    @lilydjwg @fy 统一回答一下,使用 RabbitMQ 做中间人,Redis 做后端暂时保存结果。使用 pickle 作为序列化组件。并且 `BROKER_POOL_LIMIT = 10` 。
    15 条回复    2015-05-10 09:55:26 +08:00
    est
        1
    est  
       2015-05-09 09:27:14 +08:00
    IntegrityError: (IntegrityError) (1062, u"Duplicate entry '201173000000006801' for key 'PRIMARY'")

    这个问题我遇到过,2个线程同时插入这个值,2个线程都报错,然后db里无记录,然后DB自增跳入下一个。
    ultimate010
        2
    ultimate010  
       2015-05-09 09:58:25 +08:00
    看不出问题,但是celery+supervisord爬虫完全可行,以前在20+机器用celery做任务队列的爬虫很稳定。爬虫注意try异常。
    建议用redis做broker,省得折腾。
    binux
        3
    binux  
       2015-05-09 10:41:33 +08:00
    celery 会在任务失败,没有 ack 的时候重试吧,会不会因为不完整的事务导致 Duplicate
    binux
        4
    binux  
       2015-05-09 10:41:45 +08:00
    另外,来试试 pyspider 吧
    Feiox
        5
    Feiox  
    OP
       2015-05-09 11:10:31 +08:00
    @binux 可以用于每夜白万次的生产环境吗?能不能做夸机房协同工作呢?
    binux
        6
    binux  
       2015-05-09 11:17:35 +08:00
    @Feiox 每夜算8小时吧,百万也就不到 50个请求/秒,没问题。
    Feiox
        7
    Feiox  
    OP
       2015-05-09 12:22:27 +08:00 via Android
    @binux 那跨机房协作呢。这全是一个我们用队列的一个原因。你知道国内的网络环境。
    binux
        8
    binux  
       2015-05-09 12:25:17 +08:00
    @Feiox 和 Celery 一样是消息队列,由调度器监控任务执行,超时自动重试。
    no13bus
        9
    no13bus  
       2015-05-09 12:39:42 +08:00
    @binux celery有retry机制,没错。可以在设定的时间内重启一次
    Moker
        10
    Moker  
       2015-05-09 12:58:40 +08:00
    @binux 不知道发现这个pyspider文档没有scrapy全啊....现在在用scrapy...也遇到坑了
    binux
        11
    binux  
       2015-05-09 13:37:13 +08:00
    @Moker pyspider 还很年轻。。
    lilydjwg
        12
    lilydjwg  
       2015-05-09 13:39:02 +08:00
    这用的什么 broker 啊,不会是 MySQL 吧?
    换 Redis 吧。
    fy
        13
    fy  
       2015-05-09 14:22:04 +08:00
    celery 用 Redis 妥妥的啊。
    多进程方案的话,LZ也可以试试zeromq,几十行代码就能搭建一个原型。
    跨机房也是OK的,只要能建立Socket连接就行。
    Daniel65536
        14
    Daniel65536  
       2015-05-09 23:56:30 +08:00
    @Moker pyspider 就是 binux 巨巨的作品……直接问就是 =_=
    Moker
        15
    Moker  
       2015-05-10 09:55:26 +08:00
    @Daniel65536 哈哈...看资料的时候发现了...不过话说我的问题比较新手...问多了还是比较尴尬的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5665 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 06:35 · PVG 14:35 · LAX 23:35 · JFK 02:35
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.