Exemplo n.º 1
0
def read_from_stream(stream, timeout=5):
    start = time.time()
    while time.time() - start < timeout:
        try:
            data = stream.get_nowait()
            raise tornado.gen.Return(u(data['data']))
        except Empty:
            yield tornado_sleep(0.1)
    raise TimeoutException('Timeout reading queue')
Exemplo n.º 2
0
    def read(self, timeout=None):
        timeout = timeout or self._timeout

        if self._buffer:
            raise tornado.gen.Return(self._buffer.pop(0))

        start = time.time()
        while time.time() - start < timeout:
            try:
                msg = self._stream.get_nowait()
                lines = [l for l in s(msg['data']).split('\n') if l]
                self._buffer.extend(lines)
                raise tornado.gen.Return(self._buffer.pop(0))
            except Empty:
                yield tornado_sleep(0.1)
        raise TimeoutException('Timeout reading queue')
Exemplo n.º 3
0
def read_from_stream(stream, desired_channel, timeout=5):
    start = time.time()
    accumulator = ''
    while not channels[desired_channel] and time.time() - start < timeout:
        try:
            data = stream.get_nowait()
            data = s(data['data']).split('\n')
            accumulator += data.pop(0)
            if data:
                data.insert(0, accumulator)
                accumulator = data.pop()
                for line in data:
                    if len(line) > 1 and line[1] == ':':
                        channel, string = line.partition(':')[::2]
                        channels[int(channel)].append(string)
        except Empty:
            yield tornado_sleep(0.1)
    if channels[desired_channel]:
        raise tornado.gen.Return(channels[desired_channel].pop(0))
    raise TimeoutException('Timeout reading queue')