4

I use Celery with Django to put my pdf generation in background, while I display a loading page.

But when the task is complete (i.e. my pdf is generated), I want to redirect to the next view which is responsible to send mail and display a friendly confirmation message to the user.

I know i can get the task_postrun or task_success signal, but I can't redirect from there.

I searched for hours but didn't find any solution, any ideas ?

Thanks !

1 Answer 1

3

There are two ways:

  1. Ask the server: save the task_id in the model where you are storing the PDF, and create an ajax view to check every X seconds if task is completed, the result of this view will determine if it should redirect or still wait for the PDF.

    result = MyTask.AsyncResult(task_id)
    result.get()
    
  2. Real-time web: another way is using pusher with pusher_client_python, when PDF generation is completed (in your PDF creation rutine), make a api call to pusher who will send a notification to the connected client (that one waiting for the result) and will redirect, this approach is more convenient because you don't have to be asking the server every X seconds. You will need to learn about sockets paradigm, but its very easy to implement.

Hope this helps.

3
  • 1
    Thanks ! But the first solution seems suboptimal to me, isn't there a way to use celery api or a django feature to redirect ? As for the second solution, I would rather not use a third party web service for such a simple thing :) Jul 19, 2013 at 12:06
  • you need to have the control to the request to make a redirect, after you have made the "generating the document" response page, you dont have the control of this request anymore to make it, you have to ask to the server (1) or do some realtime push (2).
    – panchicore
    Jul 20, 2013 at 22:25
  • Thanks, I just went with the ajax request ;) Jul 31, 2013 at 15:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.