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 2503
    SELinux 基础命令
    Zend Framework中的MVC架构
    phpfpm详解
    CentOS 6 minimal 安装
    php 5.3.3 中的phpfpm配置
  • 原文地址:https://www.cnblogs.com/eliwen/p/12000153.html
Copyright © 2011-2022 走看看