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.