Anonymous avatar Anonymous committed cd44e5d

Clean-up sessions in different thread (to avoid blocking the request)

Comments (0)

Files changed (1)

cherrypy/filters/sessionfilter.py

 import random
 import sha
 import time
+import thread
 import threading
 import types
 
         # Check if we need to clean up old sessions
         if cherrypy._session_last_clean_up_time + clean_up_delay < now:
             cherrypy._session_last_clean_up_time = now
-            sess.session_storage.clean_up()
+            # Run clean_up function in other thread to avoid blocking
+            #   this request
+            thread.start_new_thread(sess.session_storage.clean_up, (sess,))
         
         # Check if request came with a session ID
         if cookie_name in cherrypy.request.simple_cookie:
         cherrypy._session_lock_dict[id].release()
         sess.locked = False
     
-    def clean_up(self):
-        sess = cherrypy.request._session
+    def clean_up(self, sess):
         to_be_deleted = []
         now = datetime.datetime.now()
         for id, (data, expiration_time) in cherrypy._session_data_holder.iteritems():
         self._unlockFile(lockFilePath)
         sess.locked = False
     
-    def clean_up(self):
-        sess = cherrypy.request._session
+    def clean_up(self, sess):
         storagePath = cherrypy.config.get('session_filter.storage_path')
         now = datetime.datetime.now()
         # Iterate over all files in the dir/ and exclude non session files
         self.cursor.close()
         self.cursor = None
     
-    def clean_up(self):
-        sess = cherrypy.request._session
+    def clean_up(self, sess):
         now = datetime.datetime.now()
         self.cursor.execute(
             'select data from session where expiration_time < %s',
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.