I have setup celery, rabbitmq and django web server on digitalocean. RabbitMQ runs on another server where my Django app is not running. When I am trying to add the tasks to the queue using delay I am getting an error
AttributeError: ‘ChannelPromise’ object has no attribute ‘value‘
From django shell I am adding the task to my message queue.
python3 manage.py shell
JavaScript
x
7
1
Python 3.8.10 (default, Mar 15 2022, 12:22:08)
2
[GCC 9.4.0] on linux
3
Type "help", "copyright", "credits" or "license" for more information.
4
(InteractiveConsole)
5
>>> from app1.tasks import add
6
>>> add.delay(5, 6)
7
But getting error
JavaScript
1
28
28
1
Traceback (most recent call last):
2
File "/etc/myprojectenv/lib/python3.8/site-packages/kombu/utils/functional.py", line 30, in __call__
3
return self.__value__
4
AttributeError: 'ChannelPromise' object has no attribute '__value__'
5
6
During handling of the above exception, another exception occurred:
7
8
Traceback (most recent call last):
9
File "/etc/myprojectenv/lib/python3.8/site-packages/kombu/connection.py", line 446, in _reraise_as_library_errors
10
yield
11
File "/etc/myprojectenv/lib/python3.8/site-packages/kombu/connection.py", line 433, in _ensure_connection
12
return retry_over_time(
13
File "/etc/myprojectenv/lib/python3.8/site-packages/kombu/utils/functional.py", line 312, in retry_over_time
14
return fun(*args, **kwargs)
15
File "/etc/myprojectenv/lib/python3.8/site-packages/kombu/connection.py", line 877, in _connection_factory
16
self._connection = self._establish_connection()
17
File "/etc/myprojectenv/lib/python3.8/site-packages/kombu/connection.py", line 812, in _establish_connection
18
conn = self.transport.establish_connection()
19
File "/etc/myprojectenv/lib/python3.8/site-packages/kombu/transport/pyamqp.py", line 201, in establish_connection
20
conn.connect()
21
File "/etc/myprojectenv/lib/python3.8/site-packages/amqp/connection.py", line 323, in connect
22
self.transport.connect()
23
File "/etc/myprojectenv/lib/python3.8/site-packages/amqp/transport.py", line 129, in connect
24
self._connect(self.host, self.port, self.connect_timeout)
25
File "/etc/myprojectenv/lib/python3.8/site-packages/amqp/transport.py", line 184, in _connect
26
self.sock.connect(sa)
27
ConnectionRefusedError: [Errno 111] Connection refused
28
Started celery as :
JavaScript
1
2
1
celery -A myproject worker -l info
2
which gives me
JavaScript
1
34
34
1
User information: uid=0 euid=0 gid=0 egid=0
2
3
warnings.warn(SecurityWarning(ROOT_DISCOURAGED.format(
4
5
-------------- celery@ubuntu-s-1vcpu-1gb-blr1-01 v5.2.7 (dawn-chorus)
6
--- ***** -----
7
-- ******* ---- Linux-5.4.0-107-generic-x86_64-with-glibc2.29 2022-06-09 17:24:14
8
- *** --- * ---
9
- ** ---------- [config]
10
- ** ---------- .> app: myproject:0x7fd64fa5d970
11
- ** ---------- .> transport: amqp://himanshu:**@IPADDRESS2:5672/vhostcheck
12
- ** ---------- .> results:
13
- *** --- * --- .> concurrency: 1 (prefork)
14
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
15
--- ***** -----
16
-------------- [queues]
17
.> celery exchange=celery(direct) key=celery
18
19
20
[tasks]
21
. app1.tasks.add
22
23
[2022-06-09 17:24:14,309: INFO/MainProcess] Connected to amqp://himanshu:**@IPADDRESS:5672/vhostcheck
24
[2022-06-09 17:24:14,313: INFO/MainProcess] mingle: searching for neighbors
25
[2022-06-09 17:24:15,333: INFO/MainProcess] mingle: all alone
26
[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!
27
28
29
[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
30
leak, never use this setting in production environments!
31
warnings.warn('''Using settings.DEBUG leads to a memory
32
33
[2022-06-09 17:24:15,352: INFO/MainProcess] celery@ubuntu-s-1vcpu-1gb-blr1-01 ready.
34
Inside app1 project :
tasks.py
JavaScript
1
7
1
from __future__ import absolute_import, unicode_literals
2
from celery import shared_task
3
4
@shared_task
5
def add(x, y):
6
return x + y
7
settings.py
JavaScript
1
15
15
1
INSTALLED_APPS = [
2
3
'app1',
4
'django_celery_results',
5
]
6
7
CELERY_RESULT_BACKEND = 'django-db'
8
CELERY_CACHE_BACKEND = 'django-cache'
9
CELERY_BROKER_URL = 'amqp://himanshu:password@IPADDRESS:5672/vhostcheck'
10
11
CELERY_ACCEPT_CONTENT = ['application/json']
12
CELERY_TASK_SERIALIZER = 'json'
13
CELERY_RESULT_SERIALIZER = 'json'
14
CELERY_TIMEZONE = 'Europe/Amsterdam'
15
Advertisement
Answer
To ensure the app is loaded when Django starts, we need to import the Celery app we defined in myproject/init.py:
sudo nano myproject/__init__.py
JavaScript
1
5
1
# This will make sure the app is always imported when
2
# Django starts so that shared_task will use this app.
3
from .celery import app as celery_app
4
__all__ = ['celery_app']
5