Skip to content

Instantly share code, notes, and snippets.

@carlok
Last active June 27, 2019 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save carlok/aa9296c782a40e598e3791e49cd73183 to your computer and use it in GitHub Desktop.
Save carlok/aa9296c782a40e598e3791e49cd73183 to your computer and use it in GitHub Desktop.
AMQP Python3 pika 1.0.0 publish/consume examples
#!/usr/bin/env python
import pika
# At the moment (2019-04-07) the official examples provided by CloudAMQP are not working anymore
# because pika 1.0.0 has changed
# moreover, the official examples are in Python 2
# these examples have a few benefits:
# they work with python3
# they work with pika 1.0.0
# they were tested with amqps
# feel free to use them
url = 'your_amqps_string'
params = pika.URLParameters(url)
params.socket_timeout = 5
connection = pika.BlockingConnection(params)
channel = connection.channel()
channel.basic_publish(exchange='', routing_key='hello', body='Hello V3')
connection.close()
#!/usr/bin/env python
import pika
# At the moment (2019-04-07) the official examples provided by CloudAMQP are not working anymore
# because pika 1.0.0 has changed
# moreover, the official examples are in Python 2
# these examples have a few benefits:
# they work with python3
# they work with pika 1.0.0
# they were tested with amqps
# feel free to use them :)
url = 'your_amqps_string'
params = pika.URLParameters(url)
connection = pika.BlockingConnection(params)
channel = connection.channel()
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
print(" [x] Received " + str(body))
channel.basic_consume('hello', callback, auto_ack=True)
channel.start_consuming()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment