quixote publish.py,1.136,1.137

Greg Ward <gward-fVcApmY9cLvQ3/1i3zOLAti2O/[email protected]>
Newsgroups gmane.comp.web.quixote.cvs
Message-ID <[email protected]>
Update of /home/cvs/quixote
In directory hewson:/tmp/cvs-serv16288

Modified Files:
	publish.py 
Log Message:
Patch from Titus Brown, tweaked by me, to make Quixote a bit more
friendly to multi-threaded applications:
  * move start_time attribute from Publisher to HTTPRequest
  * make the current HTTPRequest an attribute of Publisher, and
    remove the '_request' global
  * add _set_request, _clear_request(), get_request() to Publisher
  * change most of the global convenience get_*() accordingly


Index: publish.py
===================================================================
RCS file: /home/cvs/quixote/publish.py,v
retrieving revision 1.136
retrieving revision 1.137
diff -u -d -r1.136 -r1.137
--- publish.py	14 Oct 2002 23:00:58 -0000	1.136
+++ publish.py	16 Oct 2002 18:44:37 -0000	1.137
@@ -210,7 +210,6 @@
     def parse_request (self, request):
         """Parse the request information waiting in 'request'.
         """
-        self.start_time = time.time()
         request.process_inputs()
 
     def start_request (self, request):
@@ -219,6 +218,21 @@
         """
         pass
 
+    def _set_request(self, request):
+        """Set the current request object.
+        """
+        self._request = request
+
+    def _clear_request(self):
+        """Unset the current request object.
+        """
+        self._request = None
+
+    def get_request(self):
+        """Return the current request object.
+        """
+        return self._request
+
     def log_request (self, request):
         """Log a request in the access_log file.
         """
@@ -228,9 +242,8 @@
             else:
                 user = "-"
             now = time.time()
-            seconds = now - self.start_time
-            now = time.strftime('%Y-%m-%d %H:%M:%S',
-                                time.localtime(time.time()))
+            seconds = now - request.start_time
+            timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(now))
 
             env = request.environ
             method = env.get('REQUEST_METHOD')
@@ -249,7 +262,7 @@
             self.access_log.write('%s %s %s %d "%s %s %s" %s %r %0.2fsec\n' %
                                    (request.environ.get('REMOTE_ADDR'),
                                     str(user),
-                                    now,
+                                    timestamp,
                                     os.getpid(),
                                     method,
                                     request_uri + query,
@@ -621,10 +634,13 @@
         try_publish() method will be called to do the work and
         exceptions will be handled here.
         """
-        global _request
-        _request = request
+        self._set_request(request)
+
+        try:
+            output = self.try_publish(request, env.get('PATH_INFO', ''))
+        finally:
+            self._clear_request()
 
-        output = self.try_publish(request, env.get('PATH_INFO', ''))
         self.log_request(request)
 
         if output and self.config.compress_pages:
@@ -737,40 +753,33 @@
 # Publisher singleton, only one of these per process.
 _publisher = None
 
-# The request currently being processed.
-_request = None
-
-# N.B. _publisher and _request should be accessed by functions only.  This
-# allows multithreaded implementations to use per thread tables for these
-# objects.
-
 def get_publisher():
     global _publisher
     return _publisher
 
 def get_request():
-    global _request
-    return _request
+    global _publisher
+    return _publisher.get_request()
 
 def get_path(n=0):
-    global _request
-    return _request.get_path(n)
+    global _publisher
+    return _publisher.get_request().get_path(n)
 
 def redirect(location, permanent=0):
-    global _request
-    return _request.redirect(location, permanent)
+    global _publisher
+    return _publisher.get_request().redirect(location, permanent)
 
 def get_session():
-    global _request
-    return _request.session
+    global _publisher
+    return _publisher.get_request().session
 
 def get_session_manager ():
     global _publisher
     return _publisher.session_mgr
 
 def get_user():
-    global _request
-    session = _request.session
+    global _publisher
+    session = _publisher.get_request().session
     if session is None:
         return None
     else:
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.