[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[minion-cvs] Add a Timeout option to the get method of Queue. This ...



Update of /home/minion/cvsroot/src/minion/lib/mixminion
In directory moria.mit.edu:/tmp/cvs-serv2269/lib/mixminion

Modified Files:
	Common.py 
Log Message:
Add a Timeout option to the get method of Queue.  This is standard in Python2.3

Index: Common.py
===================================================================
RCS file: /home/minion/cvsroot/src/minion/lib/mixminion/Common.py,v
retrieving revision 1.111
retrieving revision 1.112
diff -u -d -r1.111 -r1.112
--- Common.py	3 Sep 2003 16:13:23 -0000	1.111
+++ Common.py	13 Oct 2003 17:31:22 -0000	1.112
@@ -1546,3 +1546,36 @@
     def _clear(self):
         """Backend for _clear"""
         del self.queue[:]
+
+try:
+    q = MessageQueue()
+    q.put(3)
+    q.get(timeout=10)
+    BUILTIN_QUEUE_HAS_TIMEOUT = 1
+except TypeError:
+    BUILTIN_QUEUE_HAS_TIMEOUT = 0
+del q
+
+if BUILTIN_QUEUE_HAS_TIMEOUT:
+    TimeoutQueue = ClearableQueue
+else:
+    class TimeoutQueue(ClearableQueue):
+        """DOCDOC -- for python 2.2 and earlier."""
+        def get(self, blocking=1, timeout=None):
+            if timeout is None:
+                return MessageQueue.get(self, blocking)
+
+            # Adapted from 'Condition
+            _time = time.time
+            _sleep = time.sleep
+            deadline = timeout+_time()
+            delay = .0005
+            while 1:
+                try:
+                    return MessageQueue.get(self,0)
+                except QueueEmpty:
+                    remaining = endTime-_time()
+                    if remaining <= 0:
+                        raise
+                    delay = min(delay*2,remaining,0.2)
+                    _sleep(delay)