【问题标题】:celery worker crash when i start another当我开始另一个芹菜工人崩溃时
【发布时间】:2019-10-28 08:43:43
【问题描述】:

我将 django 与 celery 和 redis 一起使用。 我想要三个队列和三个工人。

我在 settings.py 中的 celery 设置如下所示:

CELERY_BROKER_URL = 'redis://localhost:6379'
CELERY_RESULT_BACKEND = 'redis://localhost:6379'
CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TASK_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Europe/Berlin'

# CELERY QUEUES SETUP
CELERY_DEFAULT_QUEUE = 'default'
CELERY_DEFAULT_ROUTING_KEY = 'default'
CELERY_TASK_QUEUES = (
    Queue('default', Exchange('default'), routing_key='default'),
    Queue('manually_crawl', Exchange('manually_crawl'), routing_key='manually_crawl'),
    Queue('periodically_crawl', Exchange('periodically_crawl'), routing_key='periodically_crawl'),
)
CELERY_ROUTES = {
    'api.tasks.crawl_manually': {'queue': 'manually_crawl', 'routing_key': 'manually_crawl',},
    'api.tasks.crawl_periodically': {'queue': 'periodically_crawl', 'routing_key': 'periodically_crawl',},
    'api.tasks.crawl_firsttime': {'queue': 'default', 'routing_key': 'default',},
}

稍后我将使用 celery multi 启动 worker,但在开发阶段我想手动启动 worker 以查看错误。

我用redis-server 启动redis 服务器,然后我用默认启动第一个worker:

celery -A proj worker -Q default -l debug -n default_worker

如果我尝试在新终端中启动下一个工作人员:

celery -A proj worker -Q manually_crawl -l debug -n manually_crawl

我在第一个默认工作终端中收到错误:

[2019-10-28 09:32:58,284: INFO/MainProcess] sync with celery@manually_crawl
[2019-10-28 09:32:58,290: ERROR/MainProcess] Control command error: OperationalError("\nCannot route message for exchange 'reply.celery.pidbox': Table empty or key no longer exists.\nProbably the key ('_kombu.binding.reply.celery.pidbox') has been removed from the Redis database.\n")
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/kombu/connection.py", line 439, in _reraise_as_library_errors
    yield
  File "/usr/local/lib/python3.7/dist-packages/kombu/connection.py", line 518, in _ensured
    return fun(*args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/kombu/messaging.py", line 203, in _publish
    mandatory=mandatory, immediate=immediate,
  File "/usr/local/lib/python3.7/dist-packages/kombu/transport/virtual/base.py", line 605, in basic_publish
    message, exchange, routing_key, **kwargs
  File "/usr/local/lib/python3.7/dist-packages/kombu/transport/virtual/exchange.py", line 70, in deliver
    for queue in _lookup(exchange, routing_key):
  File "/usr/local/lib/python3.7/dist-packages/kombu/transport/redis.py", line 877, in _lookup
    exchange, redis_key))
kombu.exceptions.InconsistencyError:
Cannot route message for exchange 'reply.celery.pidbox': Table empty or key no longer exists.
Probably the key ('_kombu.binding.reply.celery.pidbox') has been removed from the Redis database.


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/celery/worker/pidbox.py", line 46, in on_message
    self.node.handle_message(body, message)
  File "/usr/local/lib/python3.7/dist-packages/kombu/pidbox.py", line 145, in handle_message
    return self.dispatch(**body)
  File "/usr/local/lib/python3.7/dist-packages/kombu/pidbox.py", line 115, in dispatch
    ticket=ticket)
  File "/usr/local/lib/python3.7/dist-packages/kombu/pidbox.py", line 151, in reply
    serializer=self.mailbox.serializer)
  File "/usr/local/lib/python3.7/dist-packages/kombu/pidbox.py", line 285, in _publish_reply
    **opts
  File "/usr/local/lib/python3.7/dist-packages/kombu/messaging.py", line 181, in publish
    exchange_name, declare,
  File "/usr/local/lib/python3.7/dist-packages/kombu/connection.py", line 551, in _ensured
    errback and errback(exc, 0)
  File "/usr/lib/python3.7/contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "/usr/local/lib/python3.7/dist-packages/kombu/connection.py", line 444, in _reraise_as_library_errors
    sys.exc_info()[2])
  File "/usr/local/lib/python3.7/dist-packages/vine/five.py", line 194, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.7/dist-packages/kombu/connection.py", line 439, in _reraise_as_library_errors
    yield
  File "/usr/local/lib/python3.7/dist-packages/kombu/connection.py", line 518, in _ensured
    return fun(*args, **kwargs)
  File "/usr/local/lib/python3.7/dist-packages/kombu/messaging.py", line 203, in _publish
    mandatory=mandatory, immediate=immediate,
  File "/usr/local/lib/python3.7/dist-packages/kombu/transport/virtual/base.py", line 605, in basic_publish
    message, exchange, routing_key, **kwargs
  File "/usr/local/lib/python3.7/dist-packages/kombu/transport/virtual/exchange.py", line 70, in deliver
    for queue in _lookup(exchange, routing_key):
  File "/usr/local/lib/python3.7/dist-packages/kombu/transport/redis.py", line 877, in _lookup
    exchange, redis_key))
kombu.exceptions.OperationalError:
Cannot route message for exchange 'reply.celery.pidbox': Table empty or key no longer exists.
Probably the key ('_kombu.binding.reply.celery.pidbox') has been removed from the Redis database.

为什么?

【问题讨论】:

    标签: django redis celery django-celery


    【解决方案1】:

    kombu 库当前存在问题,根据这篇文章,通过降级到 4.6.4 并且对于某些人来说 4.6.3 它解决了问题

    jorijinnall commented 11 days ago
    Had the same issue.
    I fixed by downgrading kombu from 4.6.5 to 4.6.3
    I still had the bug in version 4.6.4
    

    link github

    【讨论】:

      【解决方案2】:

      您可以启动多个工人,如下所示:

      $ celery -A proj worker -l info --concurrency=4 -n wkr1@hostname
      $ celery -A proj worker -l info --concurrency=2 -n wkr2@hostname
      $ celery -A proj worker -l info --concurrency=2 -n wkr3@hostname
      

      在上面的例子中,有 3 个 worker 可以产生 4,2,2 个子进程。通常建议每台机器运行一个worker,并发值将定义并行运行的进程数。

      这些进程的默认数量通常等于该机器上的核心数量。

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-04
        • 2021-09-23
        • 2019-12-06
        • 1970-01-01
        • 2017-08-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多