Long polling in Django
Asked Answered
S

1

5

I'm using a sort of long polling in my Django app to return status messages about a long operation to the client as it progresses. I do this by returning an HttpResponse object in my view function that is initialized with an iterator that returns strings. This all works great, but the iterator function is getting pretty long with tons of yields to return the status messages.

I'd like to architect this better by splitting the long operation into multiple functions, each of which returns its own status messages. But I can't see a way to do this. In other words, I have this:

def my_long_operation():
  do_something()
  yield "Did something"
  do_something_else()
  yield "Did something else"

...and I'd like to have:

def do_something():
  do_first_part_of_something()
  yield "Did first part of something"
  do_second_part_of_something()
  yield "Did second party of something"

def do_something_else():
  do_first_part_of_something_else()
  yield "Did first part of something else"
  do_second_part_of_something_else ()
  yield "Did second party of something else"

def my_long_operation():
  do_something()
  do_something_else()

Is there some way to get the yields in the second example to yield values to the caller of iterator? If not, is there a better approach? I looked at WebSockets but it doesn't seem to be fully baked yet (especially in terms of browser support). I also considered real polling of the server but that will be much more complex, so I'd like to continue to keep the open connection and stream messages across if possible.

Schooner answered 14/7, 2011 at 18:45 Comment(3)
oblige me.. but how do you expect to use this and for this to work?Tuchun
In a nutshell, I create my response object using HttpResponse(my_long_operation(), mimetype="text/plain") and in the HTML template I attach a readyStateChange listener to the request and update the HTML with new data whenever the ready state changes.Schooner
FWIW, IMHO long polling can be a pain to implement. There are certain scenario's where it's desirable but more often than not, returning a unique Id immedtiately and providing a url to poll for status is far simpler to implement. This is great if you're using a Db or singleton to preserve state. Of course, there are situations where long polling is the perfect mechanism so don't take this as gospelRetrocede
F
4

Try:

import itertools

def my_long_operation():
    return itertools.chain(do_something(), do_something_else())
Frendel answered 14/7, 2011 at 18:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.