【问题标题】:Celery: @shared_task and non-standard BROKER_URLCelery:@shared_task 和非标准 BROKER_URL
【发布时间】:2015-11-20 00:59:48
【问题描述】:

我有一个 Celery 3.1.19 设置,它使用一个包含虚拟主机的 BROKER_URL。

# in settings.py
BROKER_URL = 'amqp://guest:guest@localhost:5672/yard'

Celery 正常启动,加载任务,我在 @app.task 装饰器中定义的任务工作正常。我假设我的rabbitmq和celery在这端的配置是正确的。

我使用@shared_tasks 定义并使用app.autodiscover_tasks 加载的任务在启动时仍然正确加载。但是,如果我调用该任务,则消息最终会出现在(仍然存在的)amqp://guest:guest@localhost:5672/ 虚拟主机中。

问题:我在这里缺少什么?共享任务从哪里获得它们的实际配置。

还有更多细节

# celery_app.py

from celery import Celery

celery_app = Celery('celery_app')
celery_app.config_from_object('settings')

celery_app.autodiscover_tasks(['connectors'])

@celery_app.task
def i_do_work():
    print 'this works'

在 connector/tasks.py 中(在同一文件夹中带有 __init__.py):

# in connectors/tasks.py

from celery import shared_task

@shared_task
def I_do_not_work():
    print 'bummer'

shared 任务也再次被 Celery 实例接收。它只是缺少将消息发送到正确 BROKER_URL 的上下文。

顺便说一句。为什么 shared_tasks 如此纯粹地记录在案。他们是否依赖于一些 Django 上下文?我没有使用 Django。

或者我需要在我的设置中添加其他参数吗?

非常感谢。

【问题讨论】:

  • 问题似乎并不局限于共享任务。只是单独文件中的任务似乎表现相同。问题是任务从哪里获得上下文。

标签: python rabbitmq celery-task


【解决方案1】:

在应用程序启动时尚未导入 celery_app。在我的项目中,我将以下代码添加到 __init__.py 与我的 celery_app 定义相同的模块级别。

from __future__ import absolute_import

try:
    from .celery_app import celery_app
except ImportError:
    # just in case someone develops application without 
    # celery running    
    pass

Celery 似乎带有一个完美运行的默认应用程序,这让我感到困惑。在这种情况下,带有 NotImplementedError 的更多类似接口的结构可能会更有帮助。尽管如此,Celery 还是很棒的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-15
    • 2019-10-17
    • 2021-04-01
    • 2020-08-25
    • 2021-12-16
    • 2012-04-30
    • 2016-02-16
    • 1970-01-01
    相关资源
    最近更新 更多