zoukankan      html  css  js  c++  java
  • python消费rabbitmq

    安装pika模块

    pip install pika==0.13.1
    
    # github
    https://github.com/pika/pika/
    
    # 官网
    https://pika.readthedocs.io/en/stable/

    消费者脚本

    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    # @time: 2021/1/28 9:39 下午
    """
        pip install pika==0.13.1
    """
    import sys
    import pika
    
    mq_host = '127.0.0.1'
    mq_vhost = '/'
    mq_queue_name = 'q_test_log'
    mq_username = 'test'
    mq_password = 'test123'
    
    
    def callback(ch, method, properties, body):
        # print(" [x] Received body %r" % body)
        body = body.decode('utf-8')
        try:
            print('消费数据 -> ', body)
        except Exception as e:
            print("error ->", e)
    
        ch.basic_ack(delivery_tag=method.delivery_tag)
    
    
    def main():
        # print(' [*] Waiting for messages. To exit press CTRL+C')
        try:
            credentials = pika.PlainCredentials(mq_username, mq_password)
            connection = pika.BlockingConnection(pika.ConnectionParameters(mq_host, 5672, mq_vhost, credentials))
        except Exception as e:
            print('连接mq失败', e)
            sys.exit(1)
    
        channel = connection.channel()
        channel.basic_qos(prefetch_count=1)
        channel.basic_consume(callback, queue=mq_queue_name)
        channel.start_consuming()
    
    
    if __name__ == '__main__':
        main()
  • 相关阅读:
    java常见面试题汇总(一)
    我的自学之路:java学习路线图分享
    bzoj3714 [PA2014]Kuglarz
    cf478D Red-Green Towers
    cf478C Table Decorations
    cf478B Random Teams
    cf479A Expression
    cf479C Exams
    cf479D Long Jumps
    cf479E Riding in a Lift
  • 原文地址:https://www.cnblogs.com/root0/p/15701547.html
Copyright © 2011-2022 走看看