I'm developping an API where users can send me POST requests. When requests are received, I want a queue to process their.
When my data requests are pushed on the celery Queue, I want my workers (remote server) to handle their.
For this, I use:
- celery version 5.1.0 for workers
- RabbitMQ version 3.9 for messages
I want my code to follow Class Programming methods so I would like to use Celery.Task class.
My code belwo:
- server.py: is my FASTAPI application & endpoints. It should handle my task called client
- tasks.py: is my celery.Celery instance & celery.Task classes.
# server.py
# Module imports
from fastapi import (
FastAPI,
Request
)
from fastapi.responses import (
JSONResponse
)
import uvicorn
# Own module
from tasks import (
client,
add
)
# Environment
app = FastAPI()
# Endpoints
@app.post('/post/{name}')
def get_post(request: Request, name: str):
result_not_working = client.delay(1, 2) # Not working
result_working = add.delay(1, 2) # Working
return JSONResponse(status_code=200, content={"status": 200, "message": f"You post {result.get()}"})
if __name__ == "__main__":
uvicorn.run(app, host='', port=)
# tasks.py
from celery import (
Celery,
Task
)
# Environment
app = Celery('test_celery',
broker=f"amqp://RABBITMQ_USER:RABBITMQ_PWD@RABBITMQ_HOST:RABBITMQ_PORT/RABBITMQ_DB",
backend='rpc://')
# Class
class Client(Task):
name = "tasks.PandasClient"
def run(self, x, y, **kwargs):
return x + y
@app.task(name="add")
def add(x, y):
return x + y
client = Client()
app.tasks.register(client)
There is my problem: When I called my client object in server.py (client is an instance of Client, which is registered into my Celery queue), i get the error:
NotImplementedError: No result backend is configured.
BUT when I import and call my add function in server.py, it works with no problems.
Strange thing, when I run my worker with the command celery -A tasks worker --loglevel=info, I can see:
It tells me my broker url is correctly set, so why not my backend (which works when I call the function add not the class Client.run)
The documentation does not explain how to build Task class
