芹菜用错了b

2024-06-17 13:16:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我从docs开始设置

在芹菜.py文件:

    from __future__ import absolute_import

    import os

    from celery import Celery

    from django.conf import settings

    # set the default Django settings module for the 'celery' program.
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cars.settings')

    app = Celery('cars')

    # Using a string here means the worker will not have to
    # pickle the object when using Windows.
    app.config_from_object('django.conf:settings')
    app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)

主管配置

^{pr2}$

django设置:

 BROKER_URL = 'redis://localhost:6379/0'

当我启动worker时,似乎一切正常,使用的代理url正确:

(env)bbay@djproj:/opt/webapps/bbay$ celery -A cars.celery:app beat
    celery beat v3.1.17 (Cipater) is starting.
    __    -    ... __   -        _
    Configuration ->
        . broker -> redis://localhost:6379/0
        . loader -> celery.loaders.app.AppLoader
        . scheduler -> celery.beat.PersistentScheduler
        . db -> celerybeat-schedule
        . logfile -> [stderr]@%INFO
        . maxinterval -> now (0s)

我的任务是:

@shared_task
def send_mail_task(template, context, send_to):
  ....

下面是我如何使用它:

  send_mail_task.delay('email/confirmation_message.html', context, [user.email, ])

但当任务调用时,它试图连接到默认代理(主机
‘127.0.0.1:5672’)。这里是stacktrace:

    Stacktrace (most recent call last):

      File "django/core/handlers/base.py", line 111, in get_response
        response = wrapped_callback(request, *callback_args, **callback_kwargs)
      File "django/views/decorators/csrf.py", line 57, in wrapped_view
        return view_func(*args, **kwargs)
      File "django/views/generic/base.py", line 69, in view
        return self.dispatch(request, *args, **kwargs)
      File "rest_framework/views.py", line 452, in dispatch
        response = self.handle_exception(exc)
      File "rest_framework/views.py", line 449, in dispatch
        response = handler(request, *args, **kwargs)
      File "accounts/api/views.py", line 132, in post
        send_mail_task.delay('email/contact_seller.html', context, [profile.user.email, ])
      File "celery/app/task.py", line 453, in delay
        return self.apply_async(args, kwargs)
      File "celery/app/task.py", line 555, in apply_async
        **dict(self._get_exec_options(), **options)
      File "celery/app/base.py", line 355, in send_task
        reply_to=reply_to or self.oid, **options
      File "celery/app/amqp.py", line 305, in publish_task
        **kwargs
      File "kombu/messaging.py", line 168, in publish
        routing_key, mandatory, immediate, exchange, declare)
      File "kombu/connection.py", line 457, in _ensured
        interval_max)
      File "kombu/connection.py", line 369, in ensure_connection
        interval_start, interval_step, interval_max, callback)
      File "kombu/utils/__init__.py", line 243, in retry_over_time
        return fun(*args, **kwargs)
      File "kombu/connection.py", line 237, in connect
        return self.connection
      File "kombu/connection.py", line 741, in connection
        self._connection = self._establish_connection()
      File "kombu/connection.py", line 696, in _establish_connection
        conn = self.transport.establish_connection()
      File "kombu/transport/pyamqp.py", line 112, in establish_connection
        conn = self.Connection(**opts)
      File "amqp/connection.py", line 165, in __init__
        self.transport = self.Transport(host, connect_timeout, ssl)
      File "amqp/connection.py", line 186, in Transport
        return create_transport(host, connect_timeout, ssl)
      File "amqp/transport.py", line 299, in create_transport
        return TCPTransport(host, connect_timeout)
      File "amqp/transport.py", line 95, in __init__
        raise socket.error(last_err)

那么什么是错误的,如何让celery连接到指定的代理,它在celery文档中的何处?在


Tags: djangoinpyselfapptaskreturnline
3条回答

问题是我错过了芹菜。__init__.py应包含以下内容(来自文档):

from __future__ import absolute_import

# This will make sure the app is always imported when
# Django starts so that shared_task will use this app.
from .celery import app as celery_app

确保配置或设置文件上的代理url具有命名空间app.config_from_object('django.conf:settings', namespace='CELERY')上指定的前缀 所以把我的BROKER_URL改为CELERY_BROKER_URL

在我的例子中,错误的代理问题是由于不正确的芹菜启动命令。在

我使用了“芹菜节拍-A=myapp”,经纪人不正确。然后我把它改为“celeri-A myapp beat”,它使用了设置中正确的代理。在

相关问题 更多 >