Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ejemplo de un cliente publicador a un broker Redis
import time
import redis
import logging
from app_redis import config, exception
logging.basicConfig(level=logging.DEBUG)
def start(redis_server: redis.Redis, max_message: int) -> bool:
"""
Post a message on a Redis topic.
:param redis_server: redis.Redis
:param max_message
:return: bool
"""
logging.info(f"[##] Publishing message...")
if redis_server is None:
raise exception.PublishRedisException()
index = 0
while index < max_message:
msg = '{"message": "Test message-%d", "result": "OK"}' % index
redis_server.publish(config.TOPIC_REDIS, msg)
logging.info(f"[##] Message wroten...{index}")
index += 1
time.sleep(1)
return True
def main():
logging.info(f"## Publish ##")
publish_redis = redis.Redis(host=config.HOST_REDIS, port=config.PORT_REDIS, db=0)
logging.info(f"[##] Redis connected")
start(publish_redis, 100)
while True:
pass
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment