Manage timeouts without using signal.alarm
[pyrabbit.git] / pyrabbit.py
index 5b47b4dd273b416a44ed344cb7339b6b796b7d72..dfb9ac8f797051d6ad373962a89390c1ab85f48b 100644 (file)
@@ -22,7 +22,7 @@
 import errno
 import os
 import pika
-import signal
+import time
 import uuid
 
 class TimeoutError(Exception):
@@ -54,26 +54,21 @@ class Connection(object):
         if len(queues) == 0:
             return None
 
-        def _handle_timeout(signum, frame):
+        t_start = time.time()
+        method = None
+        i = 0
+        while method is None and (time.time()-t_start < timeout or timeout <= 0):
+            time.sleep(0.1)
+            method, properties, body = self.channel.basic_get(queues[i])
+            if i == len(queues) - 1:
+                i = 0
+            else:
+                i += 1
+
+        if method is None:
             raise TimeoutError(os.strerror(errno.ETIME))
-        signal.signal(signal.SIGALRM, _handle_timeout)
-        signal.alarm(timeout)
-
-        res = None
-        try:
-            method = None
-            i = 0
-            while method is None:
-                method, properties, body = self.channel.basic_get(queues[i])
-                if i == len(queues) - 1:
-                    i = 0
-                else:
-                    i += 1
-            res = Message(self, method, properties, body)
-        finally:
-            signal.alarm(0)
-
-        return res
+        else:
+            return Message(self, method, properties, body)
 
 
 class Queue(object):