zoukankan      html  css  js  c++  java
  • rabbitMQ 的简单模式

    生产者:

    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import pika
    
    # 创建连接对象
    connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
    # 获取频道对象
    channel = connection.channel()
    
    # 创建队列
    channel.queue_declare(queue='hello')
    
    # 向队列插入数据
    channel.basic_publish(exchange='',
                          routing_key='hello',
                          body='Hello 12334!')
    
    print("[x] Sent '生产者发送消息'")
    connection.close()
    

      

    消费者:

    # !/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import pika
    
    connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
    channel = connection.channel()
    
    # 声明队列
    channel.queue_declare(queue='hello')
    
    def callback(ch, method, properties, body):
    
        print(" [x] Received %s" % body)
        ch.basic_ack(delivery_tag=method.delivery_tag)   # 应答信号
    
    channel.basic_consume(queue='hello',
                          on_message_callback=callback,
                          # auto_ack=True)       # 无应答模式
                          auto_ack=False)     # 应答模式
    
    print(' [x] Waiting for messages. To exit press CTRL+C')
    channel.start_consuming()
    

      

  • 相关阅读:
    poj 2718 Smallest Difference
    AtCoder Beginner Contest 100 2018/06/16
    aoj 0009 Prime Number
    poj 1930 Dead Fraction
    poj 3669 Meteor Shower
    aoj 0121 Seven Puzzle
    poj 2429 GCD & LCM Inverse
    aoj 0005 GCD and LCM
    aoj 0558 Cheese
    aoj 0033 玉
  • 原文地址:https://www.cnblogs.com/eliwen/p/12000153.html
Copyright © 2011-2022 走看看