Skip to content

Instantly share code, notes, and snippets.

@e-roux
Last active November 30, 2020 22:30
Show Gist options
  • Select an option

  • Save e-roux/0042434182a15dfb68e7fbb11bf94df7 to your computer and use it in GitHub Desktop.

Select an option

Save e-roux/0042434182a15dfb68e7fbb11bf94df7 to your computer and use it in GitHub Desktop.
Pika: basic consume
#!/usr/bin/env python
import pika
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
with pika.BlockingConnection(pika.ConnectionParameters("localhost")) as connection:
channel = connection.channel()
channel.queue_declare(queue="hello")
channel.basic_consume(queue="hello", auto_ack=True, on_message_callback=callback)
print(" [*] Waiting for messages. To exit press CTRL+C")
channel.start_consuming()
#!/usr/bin/env python
import argparse
import pika
parser = argparse.ArgumentParser(description="Message to send")
parser.add_argument("msg", metavar="msg", nargs="+", type=list, help="a message to send to the queue ")
args = parser.parse_args()
MESSAGE = " ".join(["".join(w) for w in args.msg])
with pika.BlockingConnection(pika.ConnectionParameters("localhost")) as connection:
channel = connection.channel()
channel.queue_declare(queue="hello")
channel.basic_publish(exchange="", routing_key="hello", body=MESSAGE)
print(f" [x] Sent '{MESSAGE}'")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment