0

I have a Django application that has an api app that allows an incomming request to trigger a celery task while returning a response to the request. Once the celery task completes, it writes to a file.

In my app, I have:

view.py

from api.tasks import *
from codegen import celery_app

def index(request, product_id, amount_to_gen):
    response_data = {}
    generateCodes(product_id, amount_to_gen)
    response_data['result'] = 'success'
    response_data['reason'] = 'The server has successfully received your request'
    return JsonResponse(response_data)

tasks.py

from __future__ import absolute_import
from codegen import celery_app

@celery_app.task()
def generateCodes(product_id, amount_to_gen):
    **code to generate random uniqque codes then put them in a file**

Before starting a request I start Apache and also run

python manage.py celery worker -E

then send the request. The request returns the response but it does so only after it runs the generateCodes method and nothing shows on the console with the celery worker.

Question

How can I get the request to return the response and generate the code file in the background?

2 Answers 2

1
generateCodes.apply_async(args=[product_id, amount_to_gen])

If you run it like generateCodes(..) it will run it as a normal blocking method.

Sign up to request clarification or add additional context in comments.

Comments

0

When you call generateCodes(product_id, amount_to_gen), you are bypassing Celery's distributed facilities.

For the sake of convenience, Celery defines its task object so that simply calling the task like you do will just call the task, just like you call any function in Python. This is described here:

calling (__call__)

Applying an object supporting the calling API (e.g. add(2, 2)) means that the task will be executed in the current process, and not by a worker (a message will not be sent).

Note how it says "not by a worker". That's your problem. You'd have to call generateCodes.apply_async(product_id, amount_to_gen) to have your task executed by a worker.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.