Send tasks to a Celery app on a remote server

Question:

I have a server (Ubuntu Server) on the local network on ip address: 192.168.1.9.
This server is running RabbitMQ in docker.

I defined a basic Celery app:

from celery import Celery

app = Celery(
    'tasks',
    brocker='pyamqp://<username>:<password>@localhost//',
    backend='rpc://',
)

@app.task
def add(x, y):
    return x + y

Connected on the server I run the script with celery -A tasks worker --loglevel=INFO -c 2 -E

On my local laptop in a python shell I try to execute the task remotely by creating a new Celery instance with this time the ip address of my remote server.

from celery import Celery

app = Celery(
    'tasks',
    brocker='pyamqp://<username>:<password>@192.168.1.9//',
    backend='rpc://',
)

result = app.send_task('add', (2,2))
# Note: I also tried app.send_task('tasks.add', (2,2))

And from there nothing happen, the task stay PENDING for ever, I can’t see anything in the logs, it doesn’t seem the server picks up the task.
If I connect to the server and run the same commands locally (but with localhost as the address) it works fine.

What is wrong? How can I send tasks remotely?
Thank you.

Asked By: Romn

||

Answers:

The task name is your celery app module’s path + task name because you put it in that file.
Or you can start your worker with the DEBUG log, which will list all registered tasks:

celery -A tasks worker -l DEBUG

It should be

result = app.send_task('tasks.<celery_file>.add', (2,2))

But IMO you should use some API like https://flower.readthedocs.io/en/latest/api.html to have a more stable API.

Answered By: Đào Minh Hạt

Actually there was just a typo, brocker argument instead of broker.

In [1]: from celery import Celery

In [2]: app = Celery('tasks', broker='amqp://<username>:<password>@192.168.31.9:5672//', backend='rpc://')
    
In [3]: result = app.send_task('tasks.add', (2, 3))

In [4]: result.get()
Out[5]: 5
Answered By: Romn
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.