2

I need to display some analytics to the user; however, occasionally these analytics can take a while to crunch (sometimes 2-5 seconds). Instead of waiting on these results, I think I would like to have them updated in a dynamic way on the webpage.

I already have celery implemented in a limited way, but what I would like to do is send some tasks to celery, have the screen render, and once the function is complete, send the return value to the webpage to by udpated.

Once the task is created, should I monitor its status, or once its complete can I just send the json to the webpage directly? some code examples would be helpful.

Thank you.

James R
  • 4,571
  • 3
  • 30
  • 45

1 Answers1

2

"Pushing" data is very hard to do with django. It is not supported natively and the solutions are hacky at best.

There are simpler options, the most popular being ajax polling.

  1. the user could set off a request to do analytics.

  2. django queues the process and marks it as processing in storage

  3. every x seconds the user make an ajax call requesting the status of the request.

  4. when the request is finished the storage is updated to reflect this the next poll request made can retrieve the appropriate data.

HTML5 has native support for socket based communication. but django does not support it.

Django Push HTTP Response to users

Community
  • 1
  • 1
dm03514
  • 54,664
  • 18
  • 108
  • 145
  • I think what I will do is send the task_id to the webpage, immediatly have javascript call another view. In that function, have python time.sleep() in a while loop until the task is complete. Once complete, return the json object. In the meanwhile, display those spinning wheel jquery things. While still a "pull" it should feel like a push, and this will only have one additional http request rather than one per second. – James R Jun 26 '12 at 14:56
  • @JamesR I think sleep will tie up thread execution, defeating the purpose of using the message queue. – dm03514 Jun 26 '12 at 15:41
  • import time; t = task(id=task_id); while != t.successful: time.sleep(1)... it just waits until the thread has executed, it doesn't tie up the thread, no? – James R Jun 26 '12 at 15:48