From df284bf59c3d2eb7d900d39cc11df1c34558437f Mon Sep 17 00:00:00 2001 From: Javier Sancho Date: Thu, 24 Oct 2013 10:41:24 +0200 Subject: [PATCH] Manage timeouts without using signal.alarm Better and now it's possible to do threading --- pyrabbit.py | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/pyrabbit.py b/pyrabbit.py index 5b47b4d..dfb9ac8 100644 --- a/pyrabbit.py +++ b/pyrabbit.py @@ -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): -- 2.39.2