Ejemplo de cliente consumidor a un broker de Redis
import redis | |
import logging | |
import json | |
from app_redis import config, exception | |
logging.basicConfig(level=logging.DEBUG) | |
def start(redis_server: redis.Redis) -> None: | |
""" | |
Get a message from Redis topic. | |
:param redis_server: redis.Redis | |
:return: None | |
""" | |
logging.info(f"[##] Consuming message...") | |
if redis_server is None: | |
raise exception.ConsumerRedisException() | |
consume_client_topic = redis_server.pubsub() | |
consume_client_topic.subscribe(config.TOPIC_REDIS) | |
for message in consume_client_topic.listen(): | |
if message['data'] != 1: | |
data = json.loads(message['data'].decode()) | |
logging.info(f"mesagge={data['message']} result={data['result']}") | |
def main(): | |
logging.info(f"## Consumer ##") | |
redis_server = redis.Redis(host=config.HOST_REDIS, port=config.PORT_REDIS, db=0) | |
logging.info(f"[##] Redis connected") | |
start(redis_server) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment