Home > Back-end >  Celery response to be displayed in flask app
Celery response to be displayed in flask app

Time:03-09

Views.py

class UserCreate(Resource):
        def post(self):
            # try:
             celery=create_user_employee.delay(auth_header, form_data, employee_id, employee_response)
                    # from celery import current_task
                    if "exc" in celery.get:
                        # print(celery)
                        return deliver_response(False, 'Celery task failed'), 400                    
                    return deliver_response(True, message="User Creation in Progress", status=200, data=data), 200

Task.py

@celery.task(name='accounts.tasks.create_user_employee')
def create_user_employee(auth_header, form_data, employee_id, employee_response):
    try:
        # add_employee(form_data, employee_id, eid)
        if "streetLane" in form_data:
            user_id=form_data.get("personalEmail","")
            employee_address=address_post__(form_data, auth_header, user_id)
        return deliver_response(True,message="success",status=200),200
    except Exception as e:
            return deliver_response(False, str(e), status=500), 500
    

Note:I am not able to return the response to flask app from tasks.py the objective here is that i need to break the views.py func if there is any error response from tasks.py but the result is not bein able to import or print in views.py any help would be great. ...................................................................................................................................................................................

CodePudding user response:

As the celery process is a Async process, you will not get immediate response from celery job. So this code

celery=create_user_employee.delay(auth_header, form_data, employee_id, employee_response)

doesn't give you result of the task. So celery variable doesn't give any meaningful info of task getting completed. Also don't use celery as a variable name. So to fetch results of celery task,

from celery import result
res = result.AsyncResult(job_id)

So res variable has res.status and res.result to fetch status of task and result of job_id task. You can set your own job id when you call celery task. When to fetch those results is upto you. Either write a recursive function that sees the status of job periodically or try to see job status whenever the result is required.

CodePudding user response:

I would expect an Error to be thrown at the place where you have if "exc" in celery.get: ... something like TypeError: argument of type 'function' is not iterable.

Did you try to slightly change that line and have if "exc" in celery.get(): (notice the parenthesis)?

Also, giving a result of delay() a name "celery" is misleading - whoever reads your code may think that is an instance of the Celery class, which is not. That is an instance of the AsyncResult type. get() is just one of its methods.

  • Related