Python pika Simple implementation of RABBITMQ communication

Source: Internet
Author: User
Tags message queue rabbitmq

Install and start RABBITMQ on Windows

53156015

Installing the Python Pika Library

Pip Install Pika

Writing a Send message client.py

1 #Coding:utf82 3 ImportPika4 5Connection = Pika. Blockingconnection (Pika. Connectionparameters (host='localhost'))#Create a connection6Channel = Connection.channel ()#Create a channel7Channel.queue_declare (queue='Hello')#The name of the message queue is Hello8Channel.basic_publish (exchange="',9routing_key='Hello',Tenbody='Hello world!')#set Routing_key (name of message queue) and body (Sent content) One Print("[x] sent ' Hello world! '") AConnection.close ()#Close Connection

Writing a listener message queue server.py

1 #Coding:utf82 3 ImportPika4 5Connection = Pika. Blockingconnection (Pika. Connectionparameters (host='localhost'))#Create a connection6Channel = Connection.channel ()#Establish a channel7Channel.queue_declare (queue='Hello')#The consumer and the queue are bound together, and the producers and the queue are also Hello8 9 Ten defCallback (ch, method, properties, body):#callback function Get message body One     Print("[x] Received%r"%body) A  -  - Channel.basic_consume (Callback, theQueue='Hello', -no_ack=True) -  - Print('[*] waiting for messages. To exit Press CTRL + C') +Channel.start_consuming ()#Create a dead loop, listen for Message Queuing, and use CTRL + C to end the listener

Execution server.py can listen to Message Queuing and execute client.py to initiate a client to send messages to the message queue.

Python pika Simple implementation of RABBITMQ communication

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.