Method call at end of Django session

662 views
Skip to first unread message

Amarjeet Kapoor

unread,
Oct 2, 2015, 1:25:15 PM10/2/15
to django...@googlegroups.com
Want to know how and where can I implement a method in Django which is
to called automatically only when a session end ( i.e. when user close
the webapp or browser ). I want to know where that method should be
written, how to call it automatically after end of session.

I want to use this method to delete some user specific files created
after background processing in my webapp.


--
Amarjeet Singh
https://amarjeetkapoor1.wordpress.com
https://github.com/amarjeetkapoor1
https://bitbucket.org/amarjeetkapoor

Andreas Kuhne

unread,
Oct 2, 2015, 4:55:46 PM10/2/15
to django...@googlegroups.com
Hi,

I don't think you can reliably do that. Django never knows when the user closes his browser window. Because a session is stateless (there is only a connection to the server when it responds to a request). However you could do this via a bit of ajax or a pretty unreliable javascript event.

What I would do is to ping the server via javascript once every minute or so - you then update a timestamp on a session object or the user model or something like that. Then if the timestamp is older than 5 minutes, you can safely delete the temporary files.

Another way would be to add an event handler to the onbeforeunload event, see http://stackoverflow.com/questions/20853142/trying-to-detect-browser-close-event. When the event fires, you just fire an ajax call to your server that deletes the files.

HOWEVER, I know that this doesn't always fire and can be a bit tricky. But it's worth a shot.

Regards,

Andréas

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CABskhPPQV%2BPXe-ObEbb05Bk7KpbLcv1XB__jhx2d-Q54qBzSZQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Vijay Khemlani

unread,
Oct 2, 2015, 6:28:19 PM10/2/15
to django...@googlegroups.com
Also it is most likely inconsistent if the user has the same webpage opened on two different tabs

Amarjeet Kapoor

unread,
Oct 3, 2015, 5:26:56 AM10/3/15
to Django users


On Saturday, 3 October 2015 02:25:46 UTC+5:30, Andréas Kühne wrote:
Hi,

I don't think you can reliably do that. Django never knows when the user closes his browser window. Because a session is stateless (there is only a connection to the server when it responds to a request). However you could do this via a bit of ajax or a pretty unreliable javascript event.

What I would do is to ping the server via javascript once every minute or so - you then update a timestamp on a session object or the user model or something like that. Then if the timestamp is older than 5 minutes, you can safely delete the temporary files.

Another way would be to add an event handler to the onbeforeunload event, see http://stackoverflow.com/questions/20853142/trying-to-detect-browser-close-event. When the event fires, you just fire an ajax call to your server that deletes the files.

HOWEVER, I know that this doesn't always fire and can be a bit tricky. But it's worth a shot.

Regards,

Andréas




I made following assumption that I can do that on the base that their where function related to sessions like get_expiry_age(), get_expire_at_browser_close() using which I could do what it thought.

What you told I completly understand but what I am not to understand that where should I write that background code or how to trigger that function automatically when timestamp get older than some min (as you suggested ) ,which is my basic question for this thread.
 

Amarjeet Kapoor

unread,
Oct 3, 2015, 5:35:15 AM10/3/15
to Django users


On Saturday, 3 October 2015 03:58:19 UTC+5:30, Vijay Khemlani wrote:
Also it is most likely inconsistent if the user has the same webpage opened on two different tabs


Could you please elaborate related to which thing, you are telling this and how would it lead to inconsistent?

Andreas Kuhne

unread,
Oct 3, 2015, 2:54:22 PM10/3/15
to django...@googlegroups.com
Hi again,

What I would do is create a django management command (see https://docs.djangoproject.com/en/1.8/howto/custom-management-commands/) and then use cron to call it a regular intervals (5-10 minute intervals for example). 

Another way to do it would be to use celery, which is a delayed job worker and also a cron scheduler (see http://www.celeryproject.org/). Celery has a lot of other functions so if you need to use delayed jobs or need to use jobs that will be fired at intervals, I would recommend using celery, however there is a lot more to configure using celery.

Regards,

Andréas

 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.

Amarjeet Kapoor

unread,
Oct 4, 2015, 3:19:44 AM10/4/15
to Django users


On Sunday, 4 October 2015 00:24:22 UTC+5:30, Andréas Kühne wrote:
Hi again,

What I would do is create a django management command (see https://docs.djangoproject.com/en/1.8/howto/custom-management-commands/) and then use cron to call it a regular intervals (5-10 minute intervals for example). 

Another way to do it would be to use celery, which is a delayed job worker and also a cron scheduler (see http://www.celeryproject.org/). Celery has a lot of other functions so if you need to use delayed jobs or need to use jobs that will be fired at intervals, I would recommend using celery, however there is a lot more to configure using celery.

Regards,

 

Thanks, It seems these will work.I will try it and tell if it worked or any problem faced by me.
Reply all
Reply to author
Forward
0 new messages