Arsh Doda
Arsh Doda

Reputation: 324

Call celery tasks from different server

I have a django app + redis on one server and celery on another server. I want to call celery task from django app.

My task.py on Celery Server:

from celery import Celery

app = Celery('tasks')
app.conf.broker_url = 'redis://localhost:6379/0'

@app.task(bind=True)
def test():
    print('Testing')

Calling the celery task from Django Server:

from celery import Celery
celery = Celery()
celery.conf.broker_url = 'redis://localhost:6379/0'
celery.send_task('tasks.test')

I am running the celery worker using this command:

celery -A tasks worker --loglevel=INFO

When i call the celery task from django, it pings the celery server but i get the following error:

Received unregistered task of type 'tasks.test'. The message has been ignored and discarded. Did you remember to import the module containing this task? Or maybe you're using relative imports?

How to fix this or is there any way to call the task?

Upvotes: 2

Views: 1773

Answers (1)

WMRamadan
WMRamadan

Reputation: 1210

Your task should be a shared task within celery as follows:

tasks.py

from celery import Celery, shared_task

app = Celery('tasks')
app.conf.broker_url = 'redis://localhost:6379/0'

@shared_task(name="test")
def test(self):
    print('Testing')

and start celery as normal:

celery -A tasks worker --loglevel=INFO

Your application can then call your test task:

main.py

from celery import Celery
celery = Celery()
celery.conf.broker_url = 'redis://localhost:6379/0'
celery.send_task('tasks.test')

Upvotes: 3

Related Questions