[Opensrf-commits] r1181 - trunk/src/python/osrf

svn at svn.open-ils.org svn at svn.open-ils.org
Thu Dec 20 11:15:40 EST 2007


Author: erickson
Date: 2007-12-20 10:53:54 -0500 (Thu, 20 Dec 2007)
New Revision: 1181

Modified:
   trunk/src/python/osrf/ses.py
Log:
added a multisession class for agregating requets into a single recv manager

Modified: trunk/src/python/osrf/ses.py
===================================================================
--- trunk/src/python/osrf/ses.py	2007-12-20 14:13:29 UTC (rev 1180)
+++ trunk/src/python/osrf/ses.py	2007-12-20 15:53:54 UTC (rev 1181)
@@ -317,3 +317,56 @@
 
 
 
+class MultiSession(object):
+    ''' Manages multiple requests.  With the current implementation, a 1 second 
+        lag time before the first response is practically guaranteed.  Use 
+        only for long running requests.
+
+        Another approach would be a threaded version, but that would require
+        build-up and breakdown of thread-specific xmpp connections somewhere.
+        conection pooling? 
+    '''
+    class Container(object):
+        def __init__(self, req):
+            self.req = req
+            self.id = None
+
+    def __init__(self):
+        self.complete = False
+        self.reqs = []
+
+    def request(self, service, method, *args):
+        ses = ClientSession(service)
+        cont = MultiSession.Container(ses.request(method, *args))
+        cont.id = len(self.reqs)
+        self.reqs.append(cont)
+
+    def recv(self, timeout=10):
+        ''' Returns a tuple of req_id, response '''
+        duration = 0
+        block_time = 1
+        while True:
+            for i in range(0, len(self.reqs)):
+                cont = self.reqs[i]
+                req = cont.req
+
+                res = req.recv(0)
+                if i == 0 and not res:
+                    res = req.recv(block_time)
+
+                if res: break
+
+            if res: break
+
+            duration += block_time
+            if duration >= timeout:
+                return None
+
+        if req.complete:
+            self.reqs.pop(self.reqs.index(cont))
+
+        if len(self.reqs) == 0:
+            self.complete = True
+
+        return cont.id, res.content()
+



More information about the opensrf-commits mailing list