Ejemplo de cliente consumidor a un broker de Redis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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