添加任务到RabbitMQ消息队列时Django celery出错:属性错误:“ChannelPromise”对象没有属性“__value__”

6ojccjat  于 2022-11-08  发布在  RabbitMQ
关注(0)|答案(1)|浏览(301)

我已经在digitalocean上设置了celery,rabbitmq和Django web服务器。rabbitMQ运行在另一个服务器上,而我的Django应用程序没有运行。当我尝试使用延迟将任务添加到队列时,我收到了一个错误
属性错误:'ChannelPromise'对象没有属性''
从django shell中,我将任务添加到我的消息队列中。

manage.py程序

Python 3.8.10 (default, Mar 15 2022, 12:22:08)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from app1.tasks import add
>>> add.delay(5, 6)

但获取错误

Traceback (most recent call last):
  File "/etc/myprojectenv/lib/python3.8/site-packages/kombu/utils/functional.py", line 30, in __call__
    return self.__value__
AttributeError: 'ChannelPromise' object has no attribute '__value__'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/etc/myprojectenv/lib/python3.8/site-packages/kombu/connection.py", line 446, in _reraise_as_library_errors
    yield
  File "/etc/myprojectenv/lib/python3.8/site-packages/kombu/connection.py", line 433, in _ensure_connection
    return retry_over_time(
  File "/etc/myprojectenv/lib/python3.8/site-packages/kombu/utils/functional.py", line 312, in retry_over_time
    return fun(*args,**kwargs)
  File "/etc/myprojectenv/lib/python3.8/site-packages/kombu/connection.py", line 877, in _connection_factory
    self._connection = self._establish_connection()
  File "/etc/myprojectenv/lib/python3.8/site-packages/kombu/connection.py", line 812, in _establish_connection
    conn = self.transport.establish_connection()
  File "/etc/myprojectenv/lib/python3.8/site-packages/kombu/transport/pyamqp.py", line 201, in establish_connection
    conn.connect()
  File "/etc/myprojectenv/lib/python3.8/site-packages/amqp/connection.py", line 323, in connect
    self.transport.connect()
  File "/etc/myprojectenv/lib/python3.8/site-packages/amqp/transport.py", line 129, in connect
    self._connect(self.host, self.port, self.connect_timeout)
  File "/etc/myprojectenv/lib/python3.8/site-packages/amqp/transport.py", line 184, in _connect
    self.sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

开始celery 为:

celery -A myproject worker -l info

这就给了我

User information: uid=0 euid=0 gid=0 egid=0

  warnings.warn(SecurityWarning(ROOT_DISCOURAGED.format(

 -------------- celery@ubuntu-s-1vcpu-1gb-blr1-01 v5.2.7 (dawn-chorus)
---*****-----
--*******---- Linux-5.4.0-107-generic-x86_64-with-glibc2.29 2022-06-09 17:24:14
-***--- * ---
-**---------- [config]
-**---------- .> app:         myproject:0x7fd64fa5d970
-**---------- .> transport:   amqp://himanshu:**@IPADDRESS2:5672/vhostcheck
-**---------- .> results:
-***--- * --- .> concurrency: 1 (prefork)
--*******---- .> task events: OFF (enable -E to monitor tasks in this worker)
---*****-----
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery

[tasks]
  . app1.tasks.add

[2022-06-09 17:24:14,309: INFO/MainProcess] Connected to amqp://himanshu:**@IPADDRESS:5672/vhostcheck
[2022-06-09 17:24:14,313: INFO/MainProcess] mingle: searching for neighbors
[2022-06-09 17:24:15,333: INFO/MainProcess] mingle: all alone
[2022-06-09 17:24:15,349: WARNING/MainProcess] /etc/myprojectenv/lib/python3.8/site-packages/kombu/pidbox.py:70: UserWarning: A node named celery@ubuntu-s-1vcpu-1gb-blr1-01 is already using this process mailbox!

[2022-06-09 17:24:15,352: WARNING/MainProcess] /etc/myprojectenv/lib/python3.8/site-packages/celery/fixups/django.py:203: UserWarning: Using settings.DEBUG leads to a memory
            leak, never use this setting in production environments!
  warnings.warn('''Using settings.DEBUG leads to a memory

[2022-06-09 17:24:15,352: INFO/MainProcess] celery@ubuntu-s-1vcpu-1gb-blr1-01 ready.

在app1项目内:

任务.py

from __future__ import absolute_import, unicode_literals
from celery import shared_task

@shared_task
def add(x, y):
    return x + y

设置.py

INSTALLED_APPS = [
    ...
    'app1',
    'django_celery_results',
]

CELERY_RESULT_BACKEND = 'django-db'
CELERY_CACHE_BACKEND = 'django-cache'
CELERY_BROKER_URL = 'amqp://himanshu:password@IPADDRESS:5672/vhostcheck'

CELERY_ACCEPT_CONTENT = ['application/json']
CELERY_TASK_SERIALIZER = 'json'
CELERY_RESULT_SERIALIZER = 'json'
CELERY_TIMEZONE = 'Europe/Amsterdam'
41zrol4v

41zrol4v1#

为了确保在Django启动时加载应用,我们需要导入在myproject/init.py中定义的Celery应用:

sudo nano我的项目/初始化.py


# 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
__all__ = ['celery_app']

相关问题