5
votes

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.

1
oblige me.. but how do you expect to use this and for this to work?arustgi
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.Matthew Gertner
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 gospelBasic

1 Answers

4
votes

Try:

import itertools

def my_long_operation():
    return itertools.chain(do_something(), do_something_else())