Written in the front, due to the version problem of rabbitmq, the location of the parameters passed in may be different. You can check the source code and pass them in one by one
send.py
# encoding: utf-8
# Date: 2019/11/25 20:43
__author__ = 'ryan.liu'
import pika
def test(hash_value):
# 1, Connect to RabbitMq server
rabbit_username = 'admin'
rabbit_password = 'admin'
credentials = pika.PlainCredentials(rabbit_username, rabbit_password)
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='127.0.0.1', port=5672, credentials=credentials))
# channel is the channel for reading and writing messages
channel = connection.channel()
# 2, create a queue named queue
channel.queue_declare(queue='queue')
# 3, configure basic_pulish
channel.basic_publish(
'',
'queue',
hash_value)
# 4,close connection
connection.close()
# return make_response({})
receive.py
# encoding: utf-8
# Date: 2019/11/25 20:43
__author__ = 'ryan.liu'
import pika
rabbit_username = 'admin'
rabbit_password = 'admin'
credentials = pika.PlainCredentials(rabbit_username, rabbit_password)
connection = pika.BlockingConnection(pika.ConnectionParameters(host='127.0.0.1', port=5672, credentials=credentials))
channel = connection.channel()
# Declare the queue
channel.queue_declare(queue='queue')
# 3, define a callback function that the Pika library calls when a message is obtained to process the message
def callback(ch, method, properties, body):
print("receive.py: receive message", body)
# 4, receive message from queue
# channel.basic_consume(
# queue='queue',
# callback,
# no_ack=True)
channel.basic_consume(
"queue",
callback,
auto_ack=True
)
# 5,wait for message
channel.start_consuming()
I made a mistake today, which caused MQ to report an error all the time
OSError: [Errno 9] Bad file descriptor
The error code is as follows:
rabbit_username = 'admin'
rabbit_password = 'admin'
credentials = pika.PlainCredentials(rabbit_username, rabbit_password)
connection = pika.BlockingConnection(
pika.ConnectionParameters(host='127.0.0.1', port=5672, credentials=credentials))
channel = connection.channel()
channel.queue_declare(queue='queue')
def test(hash_value):
channel.basic_publish(
'',
'queue',
hash_value)
connection.close()
# return make_response({})
This is a question of the scope of the python foundation
Rabbitmq error
pika.exceptions.IncompatibleProtocolError: StreamLostError: ('Transport indicated EOF',)
Because I started the rabbitmq service with docker, I mapped the port to port 8181, but in fact, when docker started MQ, port 5672 would be started automatically. Therefore, when configuring, port should be the default instead of 8181