Last active
June 27, 2019 19:00
-
-
Save carlok/aa9296c782a40e598e3791e49cd73183 to your computer and use it in GitHub Desktop.
AMQP Python3 pika 1.0.0 publish/consume examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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