How to use the .delay() method in django-celery?

17,084

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.

Share:
17,084
fakhir hanif
Author by

fakhir hanif

Software Engineer! 1) Loves to work in python. 2) Liked django web framework. 3) Working in Odoo(Open ERP).

Updated on June 15, 2022

Comments

  • fakhir hanif
    fakhir hanif about 2 years

    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
    
  • fakhir hanif
    fakhir hanif over 10 years
    Yes. In my case i do not saving something to the database. I used its returned value in response. You are right my code doesn't wait until it has the result. So i used .wait() and it worked for me. Is this the best way for doing this? By the way I have achieved 50% better speed in getting response.
  • RemcoGerlich
    RemcoGerlich over 10 years
    Maybe if the server that runs Celery is much faster than your web server, but in general waiting for the result is not why you use Celery.
  • fakhir hanif
    fakhir hanif over 10 years
    What should i need to use then?
  • olofom
    olofom over 10 years
    You should just do the call directly if you don't want the task to run asynchronously. There's some overhead when using Celery which means that running it as a task and waiting for the result will be slower than just call a regular function which does the same.
  • fakhir hanif
    fakhir hanif over 10 years
    Actually i want to run task asynchronously. Task includes external connection to the third party. I want to make this task run parallel with other processes.
  • sattva_venu
    sattva_venu almost 4 years
    When we configure the celery worker to run on remote host, the delay() will work as synchronous call instead of asynchronous call.