Question

I am trying to call a function after a particular view is finished sending the response object to the user - so the user does not have to wait for the function to be executed.

I am trying to use request_finished of the Django Signals Framework but I do not know how to access the HttpRequest object in the kwargs that Django signal sends to my callback.

Looks like the Signal object does not contain any useful information about the request.

ALSO, is this the best way to execute a function outside the request-response cycle? I do not want to use an advanced solution like Celery at this point in time.

Was it helpful?

Solution

That signal doesn't do what you think it does. As you can see from the handler code, the request_finished signal is sent when the request has been processed, but before the response is returned to the user. So anything that you add to that signal will still happen before the user sees any of the response.

Because of the way web servers work, there's no way to run code after the response is returned to the user. Really, the only thing to do is use something like Celery - you could knock up your own version that simulates a task queue using a db table, then have a cron job pick up items from the table, but it'll be a whole lot easier just to use Celery.

OTHER TIPS

The crosstown_traffic API of hendrix, which uses Twisted to serve Django, is designed specifically to defer logic until immediately after the Response has gone out over the wire to the client.

http://hendrix.readthedocs.org/en/latest/crosstown_traffic/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top