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?