Skip to content

Instantly share code, notes, and snippets.

@e-roux
Last active November 30, 2020 22:30
Show Gist options
  • Save e-roux/0042434182a15dfb68e7fbb11bf94df7 to your computer and use it in GitHub Desktop.
Save e-roux/0042434182a15dfb68e7fbb11bf94df7 to your computer and use it in GitHub Desktop.
Pika: basic consume
#!/usr/bin/env python
import pika, sys, os
def main():
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
channel.basic_consume(queue='hello', on_message_callback=callback, auto_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
print('Interrupted')
try:
sys.exit(0)
except SystemExit:
os._exit(0)#!/usr/bin/env python
#!/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