Exemplo n.º 1
0
class TDCQueue(object):
    '''Time Delay Circular Queue
    '''
    def __init__(self, capacity=10000, delay_time=10, pop_size=1):
        self.__queue = CircularQueue(capacity=capacity)
        self.__set = set()
        self.__DELAY_TIME = delay_time
        self.__POP_SIZE = pop_size
        g_logger.info('Capacity: %d, DelayTime: %d', capacity,
                      self.__DELAY_TIME)
        pass

    def push(self, q_data):
        assert isinstance(q_data, dict), '%s is not DictType' % q_data
        assert q_data and 'k' in q_data and 'v' in q_data
        key = q_data['k']
        if key in self.__set:
            return ECODE.Q_EXISTS
        else:
            self.__set.add(key)
            q_data = (q_data, int(time.time()) + self.__DELAY_TIME)
            return self.__queue.push(q_data)
        pass

    def pop(self):
        pop_items = {}
        wait_time = self.__DELAY_TIME
        current_time = int(time.time())

        pop_count = self.__POP_SIZE
        while pop_count:
            q_item = self.__queue.head()
            if ECODE.Q_EMPTY == q_item:
                break

            g_logger.debug(q_item)

            q_data, expire_time = q_item

            if current_time >= expire_time:
                assert isinstance(q_data, dict)
                pop_items[q_data['k']] = q_data['v']
                self.__queue.pop()
                self.__set.remove(q_data['k'])
                pass
            else:
                wait_time = expire_time - current_time
                break
            pop_count -= 1
            pass
        return {'data': pop_items, 'wait_time': wait_time}

    def status(self):
        wait_time = self.__DELAY_TIME
        current_time = int(time.time())

        result = self.__queue.status()

        q_item = self.__queue.head()
        if ECODE.Q_EMPTY != q_item:
            q_data, expire_time = q_item
            wait_time = expire_time - current_time

        result['wait_time'] = wait_time

        return result
from circular_queue import CircularQueue

q = CircularQueue(5)

q.insert(1)
q.insert(2)
q.insert(3)
q.insert(4)
q.status()
q.insert(5)
q.insert(6)
q.insert(7)
q.insert(8)
q.insert(9)
print(q)
Exemplo n.º 3
0
class TDCQueue(object):
    '''Time Delay Circular Queue
    '''
    def __init__(self, capacity=10000, delay_time=10, pop_size=1):
        self.__queue = CircularQueue(capacity=capacity)
        self.__set = set()
        self.__DELAY_TIME = delay_time
        self.__POP_SIZE = pop_size
        g_logger.info('Capacity: %d, DelayTime: %d', capacity,
                self.__DELAY_TIME)
        pass

    def push(self, q_data):
        assert isinstance(q_data, dict), '%s is not DictType' % q_data
        assert q_data and 'k' in q_data and 'v' in q_data
        key = q_data['k']
        if key in self.__set:
            return ECODE.Q_EXISTS 
        else:
            self.__set.add(key)
            q_data = (q_data, int(time.time()) + self.__DELAY_TIME)
            return self.__queue.push(q_data)
        pass

    def pop(self):
        pop_items = {} 
        wait_time = self.__DELAY_TIME 
        current_time = int(time.time())

        pop_count = self.__POP_SIZE 
        while pop_count:
            q_item = self.__queue.head()
            if ECODE.Q_EMPTY == q_item:
                break

            g_logger.debug(q_item)

            q_data, expire_time = q_item

            if current_time >= expire_time:
                assert isinstance(q_data, dict)
                pop_items[q_data['k']] = q_data['v']
                self.__queue.pop()
                self.__set.remove(q_data['k'])
                pass
            else:
                wait_time = expire_time - current_time
                break
            pop_count -= 1
            pass
        return {'data': pop_items, 'wait_time': wait_time}

    def status(self):
        wait_time = self.__DELAY_TIME 
        current_time = int(time.time())

        result = self.__queue.status()

        q_item = self.__queue.head()
        if ECODE.Q_EMPTY != q_item:
            q_data, expire_time = q_item
            wait_time = expire_time - current_time

        result['wait_time'] = wait_time

        return result