Using rabbitmq message queuing

1, Foreword

In python, there is a queue in itself. One is thread queue, the other is queue in multiprocessing.

Thread queue: only used for data interaction between threads

Process Queue: used for data interaction between parent and child processes under the same process, or interaction between multiple child processes under the same parent process

2, RabbitMQ

If multiple processes (different applications) and multiple systems need data interaction, message services can be used to solve these problems. Message service is good at solving the problem of data exchange (message notification / communication) between multi system and heterogeneous system. You can also use it for the inter system service call (RPC). RabbitMQ is one of the most popular message middleware.

Introduction to RabbitMQ: Click to view

Implement the simplest queue communication

  

send end

# -*- coding: UTF-8 -*-

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
    'localhost'))

# Declare a pipeline
channel = connection.channel()

# Declare queue
channel.queue_declare(queue='hello')  # Name of the queue

# n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
channel.basic_publish(exchange='',
                      routing_key='hello',
                      body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()

receive end

# -*- coding: UTF-8 -*-

import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(
    'localhost'))
channel = connection.channel()

# You may ask why we declare the queue again ‒ we have already declared it in our previous code.
# We could avoid that if we were sure that the queue already exists. For example if send.py program
# was run before. But we're not yet sure which program to run first. In such cases it's a good
# practice to repeat declaring the queue in both programs.

# The queue name is just declared in the send side. Another declaration can ensure that the data sent by the producer can be found
# If the consumer starts before the producer, the message queue will not be found and an error will be reported
channel.queue_declare(queue='hello')


def callback(ch, method, properties, body):
    print('--->>', ch, '\n', method, '\n', properties)
    print(" [x] Received %r" % body)

# ch: declared pipeline channel object memory address
#

channel.basic_consume(callback,  # If you receive a message, call the callback function to process the message
                      queue='hello',
                      no_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')

# Always put it away. It's not stuck here
channel.start_consuming()

3, Connect to remote rabbitmq

Connecting to the remote rabbitmq may cause authentication problems. You need to enter a user name and password

Set user name, password and permission under windows: here

Set user name, password and permission in linux Environment: here

It's basically at the back

send side settings:

# -*- conding:utf-8 -*-

import pika

# Certification information
credentials = pika.PlainCredentials('bigberg', '111111')

connection = pika.BlockingConnection(pika.ConnectionParameters(
    '172.16.200.109', 5672, '/', credentials))

# Declare a pipeline
channel = connection.channel()

# Declare queue
channel.queue_declare(queue='hello')  # Name of the queue

# n RabbitMQ a message can never be sent directly to the queue, it always needs to go through an exchange.
channel.basic_publish(exchange='',
                      routing_key='hello',
                      body='from remote server')
print(" [x] Sent 'from remote server'")
connection.close()

Tags: Python RabbitMQ Windows Linux

Posted on Mon, 04 May 2020 06:43:07 -0400 by JOWP