【python】消费 redis、解析 proto

python 消费 redis,解析 proto

import json
import redis
import genericobj_pb2
import common_pb2
import uuid
from datetime import datetime

def consume_redis_message(group_name, consumer_name, stream_name):
    # 连接Redis
    r = redis.Redis(host='localhost', port=6379, db=0)

    # 创建消费组
    r.xgroup_create(stream_name, group_name, id='0', mkstream=True)

    # 实时消费最新消息
    while True:
        messages = r.xreadgroup(group_name, consumer_name, {stream_name: '>'}, count=1)
        for stream, data in messages:
            for message_id, message in data:
                for key, value in message.items():
                    # print(f"Received message: {message_id} - {key} - {value}")
                    parse_data(value)

def parse_data(message):
    # proto 解析
    gObj = genericobj_pb2.GenericObj()
    gObj.ParseFromString(message)
    allObj = genericobj_pb2.RecObjs()
    print(gObj.ObjType)

你可能感兴趣的:(#,python,python,redis,开发语言)