# create an instance of User v1
        user = UserModel(name=random.choice([
            "Juan",
            "Peter",
            "Michael",
            "Moby",
            "Kim",
        ]),
                         age=random.randint(1, 50))

        msgid = consumer_group.my_stream.add({"message": user.serialize()})
        print(f"Producing message {msgid}")

    print("Producer finished....")
    print("#" * 80)
    sleep(2)


if __name__ == "__main__":
    db = Database()
    stream_name = 'my-stream'
    db.Stream(stream_name)  # Create a new stream instance

    # create the consumer group
    consumer_group = db.consumer_group('my-consumer-group-1', [stream_name])
    consumer_group.create()  # Create the consumer group.
    consumer_group.set_id('$')

    produce(consumer_group)
    consume(consumer_group)
Example #2
0
class from_redis(Stream):
    def __init__(self,
                 topics,
                 poll_interval=0.1,
                 start=False,
                 group="test",
                 **kwargs):

        from walrus import Database
        self.consumer = None
        self.topics = topics
        self.group = group
        self.poll_interval = poll_interval
        self.db = Database()
        self.consumer = self.db.consumer_group(self.group, self.topics)
        self.consumer.create()  # Create the consumer group.
        self.consumer.set_id('$')  # 不会从头读

        super(from_redis, self).__init__(ensure_io_loop=True, **kwargs)
        self.stopped = True
        if start:
            self.start()

    def do_poll(self):
        if self.consumer is not None:
            meta_msgs = self.consumer.read(count=1)

            # Returns:
            [('stream-a', [(b'1539023088125-0', {
                b'message': b'new a'
            })]),
             ('stream-b', [(b'1539023088125-0', {
                 b'message': b'new for b'
             })]), ('stream-c', [(b'1539023088126-0', {
                 b'message': b'c-0'
             })])]

            if meta_msgs:
                l = []
                for meta_msg in meta_msgs:
                    topic, msg = meta_msg[0], meta_msg[1][0]
                    msg_id, msg_body = msg
                    msg_body = (msg_body.values() >> head(1) >>
                                to_list)[0]  # {'data':'dills'}
                    l.append(RedisMsg(topic, msg_id, msg_body))
                return l
            else:
                return None

    @gen.coroutine
    def poll_redis(self):
        while True:
            vals = self.do_poll()
            if vals:
                for val in vals:
                    self._emit(val)

            yield gen.sleep(self.poll_interval)
            if self.stopped:
                break

    def start(self):
        if self.stopped:
            self.stopped = False
            self.loop.add_callback(self.poll_redis)

    def stop(self):
        if self.consumer is not None:
            self.consumer.destroy()
            self.consumer = None
            self.stopped = True