I want to use .delay to achieve asynchronous behavior. The main reason for using this is to speed up my view. Am I doing this wrong? If so, how should I do it correctly?
Below is the example code:
View.py
@cache_page(60*60*24)
def my_view(request):
something ..... .... ....
a = SomeModel.objects.get(pk=id)
data = celery_task.delay(a)
return dumpjson(status='ok', data=data, callback=callback)
Task.py
def celery_task(a):
res = request.get('http:sample.sample.com/feed/result' params={'abc': 'abc'})
return {'response': res}
If I bring the response from celery_task it displays some guid (1b52f519-64cb-43da-844a-2886bcccb9bc) and the error is something like this:
<EagerResult: 1b52f519-64cb-43da-844a-2886bcccb9bc> is not JSON serializable
You're delaying the function and calling it asynchronously. Therefore, inevitably, your code doesn't wait until it has the result. As that is sort of the point.
So Celery will start running celery_task
in the background, and you will have to return something to the client without knowing what the result is going to be. Maybe if the task is done it can save the data to a database or so, and the next time the user goes to the page you can show the finished data from the database. Or anything else.
What you get back from .delay
is a sort of handle to the background task. You could call .get()
on it (if I recall correctly) and that will hang until it gets the return value, but then you're back to calling the function synchronously.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With