Exemplo n.º 1
0
class Stream(object):
    """
    Stream
    """

    def __init__(self):
        self.multi_queue = MulticastQueue()
        self.maxsize = MAXSIZE
        self.closed = False
        self.endpoints = list()

    def new_consumer(self):
        """
        add a consumer to the stream
        :return: an shadow id to pass by reader to get data
        """
        id = len(self.endpoints)
        endpoint = self.multi_queue.add_consumer()
        self.endpoints.append(endpoint)
        return id

    def push(self, values):
        """
        Push values in the buffer
        :param values: list of values
        :return: True if buffer can still hold data, else False
        """
        if self.closed:
            raise EndOfStream

        for value in values:
            self.multi_queue.put(value)
        return len(self.multi_queue) < self.maxsize

    def pop(self, reader_id):
        try:
            data = self.endpoints[reader_id].get()
            return data
        except EmptyQueue:
            if not self.closed:
                raise EmptyStream
            else:
                raise EndOfStream

    def size(self, reader_id):
        """
        return the size of local buffer for specified reader
        :param reader_id:
        :return:
        """
        return len(self.endpoints[reader_id])

    def close(self):
        logger.debug("close stream %s" % self)
        self.closed = True

    def __len__(self):
        return len(self.multi_queue)
Exemplo n.º 2
0
 def __init__(self):
     self.multi_queue = MulticastQueue()
     self.maxsize = MAXSIZE
     self.closed = False
     self.endpoints = list()