Tried scheduling a function in django with celery beat but giving error

418 views Asked by At

Tried to schedule a function print_hello() in Django. But code seems not working.

Here is the Django project layout. Only celery related files are given.

- celery_test
    - celery_test
        - __init__.py
        - celery.py
        - tasks.py

_init_.py code:

from .celery import app as celery_app

__all__ = ('celery_app',)

celery.py code:

import os
from celery import Celery

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'celery_test.settings')

app = Celery('celery_test')
app.config_from_object('django.conf:settings', namespace='CELERY')
app.conf.schedule_beat = {
    'print-hello-every-2-seconds': {
        'task': 'tasks.print_hello',
        'schedule': 2,
    },
}
app.autodiscover_tasks()

tasks.py code:

from celery import shared_task

@shared_task
def print_hello():
    print("Hello celery...")

After running celery -A celery_test worker -l info, prompt is showing following error.

 -------------- celery@########## v4.4.7 (cliffs)
--- ***** -----
-- ******* ---- Windows-10-10.0.18362-SP0 2020-09-26 19:03:12
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app:         celery_test:0x2c34a320eb0
- ** ---------- .> transport:   amqp://guest:**@localhost:5672//
- ** ---------- .> results:     disabled://
- *** --- * --- .> concurrency: 4 (prefork)
-- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
--- ***** -----
 -------------- [queues]
                .> celery           exchange=celery(direct) key=celery


[tasks]


[2020-09-26 19:03:13,202: INFO/SpawnPoolWorker-1] child process 4856 calling self.run()
[2020-09-26 19:03:13,202: INFO/SpawnPoolWorker-3] child process 784 calling self.run()
[2020-09-26 19:03:13,211: INFO/SpawnPoolWorker-2] child process 8868 calling self.run()
[2020-09-26 19:03:13,220: INFO/SpawnPoolWorker-4] child process 4064 calling self.run()
[2020-09-26 19:03:14,922: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [WinError 10061] No connection could be made because the target machine actively refused it.
Trying again in 2.00 seconds... (1/100)

[2020-09-26 19:03:18,937: ERROR/MainProcess] consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [WinError 10061] No connection could be made because the target machine actively refused it.
Trying again in 4.00 seconds... (2/100)

Following versions are installed.

django 3.1.1
celery 4.4.7
django-celery-beat 2.0.0

Have tried downgrade celery to 3.1.15. But django-celery-beat 2.0.0 is not compatible with that.

1

There are 1 answers

1
addu390 On BEST ANSWER

Are you running RabbitMQ? - Celery broker (You can also choose to use Redis, a commonly used celery broker). The default port of RabbitMQ is 5672.

For reference have a look at this You can also find usage of @shared_task in the above repository mentioned.