the generic answer is to use a queue service (like rabbitMQ, or lots
of others). a simplistic implementation is just stashing some
parameters into a DB table and use a cron job to 'execute' them
(called Ghetto queue in the 'scaling django' presentation).
there are some 'same process' python queues out there, mostly just a
thin wrapper on a python Queue object (which has task_done() and
join() methods just for this). but for multi-process Django i don't
think it would work.
maybe is it possible to use signals like this? i haven't checked the
source, but it seems plausible to have them dispatched _after_ the
request is serviced.
--
Javier
no, it doesn't work like that. also, it seems that a custom
middleware wouldn't work, given the way the WSGIHandler() is written
(the last thing it does is to return the response content).
go the message queue way.
--
Javier
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