Skip to content

Instantly share code, notes, and snippets.

@AlvaroMonsalveSerrano
Created March 9, 2021 13:50
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save AlvaroMonsalveSerrano/cf7c0cafa10cc846633a603dac50d699 to your computer and use it in GitHub Desktop.
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