Skip to content

Instantly share code, notes, and snippets.

@AlvaroMonsalveSerrano
Created March 9, 2021 13:51
Show Gist options
  • Save AlvaroMonsalveSerrano/5712ee02353f7d100580f3f52111e220 to your computer and use it in GitHub Desktop.
Save AlvaroMonsalveSerrano/5712ee02353f7d100580f3f52111e220 to your computer and use it in GitHub Desktop.
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