On Thu, 2009-07-09 at 13:19 -0700, Fluoborate wrote:
> Hello All,
> 
> I am afraid that I might have to write my own middleware to do this. I
> really want to do something like the following:
> 
> #In views.py:
> 
> def welcome( request ):
>     return HttpResponse( "Welcome to my website" )
>     #No code after the 'return' statement will ever be executed, but I
> wish it would:
>     time-consuming_function( request )
> 
> Why do I want to do this? My website feels a bit slow to use, because
> it needs to do time-consuming_function. The content of the HTTP
> response does not depend on time-consuming_function, so why should the
> user have to wait for it? Unfortunately, time-consuming_function DOES
> depend on the HttpRequest object and other local variables only
> available in the scope of the view function, so it is much more
> convenient to execute time-consuming_function inside the view
> function.
> 
> Here is how I could do it in middleware, if nobody has an easier
> solution:
> 
> @this_view_function_has_a_callback
> def welcome( request ):
>     return HttpResponse( "Welcome to my website" ), time-
> consuming_function, { "request" : request }
> 
> I would have to write middleware to unpack the tuple returned by the
> view function and do the proper stuff with each piece. What a hassle.
> 
> Has anyone already written that? Is there some much smarter and more
> graceful work-around? Thank you all so much.

Would doing the expensive function in a generator be enough? Eg:

def welcome(request):
  def worker():
    yield "Welcome to my website"
    yield time_consuming_function(request)
  return HttpResponse(worker())

The user will see the 'Welcome to my website' immediately, and the page
will continue to load in the background, performing your time consuming
function.

Cheers

Tom


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to