I am trying to make a task with celery using redis as broker and backend with fastapi.
I am new to this so I do not know what am I doing wrong.
I am following this tutorial: https://www.youtube.com/watch?v=mcX_4EvYka4
I am also giving my own code below:
.env
CELERY_BROKER_URL=redis://localhost:6379/0
CELERY_RESULT_BACKEND=redis://localhost:6379/0
celery_worker.py
import os
import time
from celery import Celery
from dotenv import load_dotenv
load_dotenv()
celery = Celery(__name__)
celery.conf.broker_url = os.environ.get("CELERY_BROKER_URL")
celery.conf.result_backend = os.environ.get("CELERY_RESULT_BACKEND")
@celery.task(name="create_task")
def create_tasks(a, b, c):
print(a, b, c)
time.sleep(a)
return b + c
app.py
from fastapi import FastAPI, Body
# Initializes FastAPI
app = FastAPI()
from celery_worker import create_tasks
from fastapi.responses import JSONResponse
@app.post("/test")
def run_task(data=Body(...)):
amount = int(data["amount"])
x = int(data["x"])
y = int(data["y"])
task = create_tasks.delay(amount, x, y)
return JSONResponse({"Result": task.get()})
I am using flower
to view the tasks in the browser.
When I am excecuting the /test
endpoint a task is created. But it is not excecuting.
The command I am using to run celery and flower is below:
celery -A celery_worker.celery worker --loglevel=INFO
celery -A celery_worker.celery flower --port=5555
What could be the problem here?
Thanks
2
Answers
After following this answer it worked: https://stackoverflow.com/a/66824693/11348853
But I do not know why.
All I have to do is install eventlet
And then
N.B: Same thing can be done by using
gevent
. Just replaceeventlet
withgevent
.Unlike FastAPI, you should only pass the module name on the command line, not the
Celery
object. So to start the worker, run