Python3 basic usage of RabbitMQ through Kombu 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 x64kombu:4.1.0


"View queue status"

# View queue status via browser Http://192.168.0.xx:15672/api/queues/vhosttest/queuetest # View queue status from the command line curl-u User:password/http/  192.168.0.xx:15672/api/queues/vhosttest/queuetest | JQ # View Queue Length via command line (messages = Messages_ready + messages_unacknowledged) curl-s-u user:password http://192.168.0.xx:15672/ Api/queues/vhosttest/queuetest | \ JQ '. Messages '


"Send.py"

#encoding: utf-8#author: walker#date: 2018-03-09#summary:  Sender/producer Import os, sys,  timefrom kombu import connectiondef main (): With connection (' Amqp://test:[email  protected]:5672/vhosttest ')  as conn:with conn.channel ()  as channel: #producer  = producer (channel) Producer = channel. Producer () while true:message = time.strftime ('%h:%m:%s ',  time.localtime ()) Producer.publish (body=message,retry=true,exchange= ' exchangetest ', routing_key= ' rkeytest ') print (' send message: %s '  %  message)      while True:         #  Check queue to get back message Count Queue = channel.queue_declare (queue= ' queuetest ',  passive=true)      messagecount = queue.message_countprint (' messageCount: %d '  %  messagecount) IF MESSAGECOUNT < 100:BREAKTIME.SLeep (1) if __name__ ==  ' __main__ ': Main () 


"Recv.py"

#encoding: utf-8#author:walker#date:2018-03-09#summary: Receiver/Consumer import OS, sys, timefrom kombu import Connection, Queuef Rom kombu.mixins import consumermixinclass C (consumermixin):d ef __init__ (self, Connection, Queuenmae): self.connection = Connectionself.queues = [Queue (Queuenmae, Durable=false)]def get_consumers (self, Consumer, channel): return [Consumer (Self.queues, Callbacks=[self.on_message]),]# receives the callback function that handles the message def on_message (self, Body, message):p rint ("Received%s"% Body) Message.ack () def Main (): With Connection (' Amqp://test:[email protected]:5672/vhosttest ') as CONN:C (Conn, ' Queuetest '). Run () if __name__ = = ' __main__ ': Main ()


"Related reading"

    • Kombu PyPI

    • Kombu Readthedocs

    • Kombu Study Notes (i)

    • Python3 basic usage of RabbitMQ through Pika connection


Walker * * *



Python3 basic usage of RabbitMQ through Kombu 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.