Exemple #1
0
 def __init__(self, threshold=0):
     self._threshold = threshold
     self._listeners = []
     self._q = []
     self._underflow = Condition(Lock())
     self._overflow = Condition(Lock())
Exemple #2
0
class Queue:
    def __init__(self, threshold=0):
        self._threshold = threshold
        self._listeners = []
        self._q = []
        self._underflow = Condition(Lock())
        self._overflow = Condition(Lock())
    def get_threshold(self):
        return self._threshold
    def set_threshold(self, threshold):
        self._threshold = threshold
    def put(self, object, timeout=None):
        self._underflow.acquire()
        if self._threshold and len(self._q) >= self._threshold:
            self._overflow.acquire()
            self._underflow.release()
            self._overflow.wait(timeout)
            self._overflow.release()
            self._underflow.acquire()
            if len(self._q) >= self._threshold:
                self._underflow.release()
                raise QueueFull()
        self._q.append(object)
        self._underflow.notify()
        self._underflow.release()
        for listner in self._listeners:
            listner.notify()
        return
    def get(self, timeout=None):
        original_timeout = timeout
        result = NOTHING
        self._underflow.acquire()
        t_lapsed = 0
        t_start = time.time()
        while not len(self._q):
            if timeout != None:
                timeout = original_timeout - t_lapsed
                if timeout <= 0:
                    break
            self._underflow.wait(timeout)
            t_lapsed = time.time() - t_start
        else:
            result = self._q.pop(0)
        self._overflow.acquire()
        self._overflow.notify()
        self._overflow.release()
        self._underflow.release()
        return result
    def add_listener(self, listener):
        self._listeners.append(listener)