Python3 basic usage of RabbitMQ through Pika connection

Source: Internet
Author: User
Tags rabbitmq

"RabbitMQ Server"

# inside the vhosttest there are exchangetest and queuetest through rkeytest bindings broker:192.168.0.xxvirtual host:vhosttestExchange:exchangetest Queue:queuetest Routing Key:rkeytest


"Python Environment"

Os:windows 10python:3.6.3 x64pika:0.11.2


"View queue status through browser"

Http://192.168.0.xx:15672/api/queues/vhosttest/queuetest


"Send.py"

#encoding: utf-8#author: walker#date: 2018-01-31#summary:  Sender/producer Import os, sys,  timeimport pikadef main (): Credentials = pika. Plaincredentials ("Test",  "test") Parameters = pika. Connectionparameters (host= "192.168.0.xx",  virtual_host= ' vhosttest ',  credentials=credentials) Connection = pika. Blockingconnection (parameters)     #  connectivity  RabbitMQchannel =  Connection.channel ()           #  Create channel queue =  Channel.queue_declare (queue= ' queuetest ')      #  declare or create a queue while true:   #  loop sends messages to the queue message = time.strftime ('%h:%m:%s ',  time.localtime ()) channel.basic_ Publish (exchange= ' exchangetest ',   routing_key= ' rkeytest ', body=message) print (' Send message:  %s '  %  message)      while true:   #  Check the queue to get the message count back Queue = channel.queue_declare (queue= ' queuetest ',  passive=true)       messagecount = queue.method.message_countprint (' messagecount: %d '  %  messagecount) If messagecount < 100:breaktime.sleep (1) #  close connection connection.close () if  __name__ ==  ' __main__ ': Main ()


"Recv.py"

#encoding: utf-8#author: walker#date: 2018-01-31#summary:  Receiver/consumer Import os, sys,  timeimport pika#  receive callback function for processing messages def consumercallback  (channel, method,  Properties, body):p rint ("received %s"  % body) Def main (): Credentials = pika. Plaincredentials ("Test",  "test") Parameters = pika. Connectionparameters (host= "192.168.0.xx",  virtual_host= ' vhosttest ',  credentials=credentials) Connection = pika. Blockingconnection (parameters)     #  connectivity  RabbitMQchannel =  Connection.channel ()           #  Create channel queue =  Channel.queue_declare (queue= ' queuetest ')      #  declare or create a queue # no_ack=true  Turn on automatic confirmation, or the message after consumption will remain in the queue # no_ack = no_manual_ack = auto_ack; do not manually answer, Turn on auto answer mode Channel.basic_consume (consumercallback, queue= ' queuetest ', no_ack=true) print (' wait message  ... ') channel.start_consuming () if __name__ ==  ' __main_ _ ': Main ()


"Related reading"

    • Pika PyPI

    • Pika Readthedocs

    • Ubuntu 16.04 installs RabbitMQ via apt

    • The RABBITMQ of Python3


Walker * * *


Python3 basic usage of RabbitMQ through Pika connection

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.