zoukankan      html  css  js  c++  java
  • Python-RabbitMQ-fanout(广播模式)

    生产者:fanout_publiser.py

    import pika
    import sys
    connection = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
    
    channel = connection.channel()
    #有的机器type不行,换成exchange_type就没问题了
    channel.exchange_declare(exchange='logs',
                             exchange_type='fanout')
    
    message = ' '.join(sys.argv[1:]) or "info: Hello World!"
    channel.basic_publish(exchange='logs',
                          routing_key='',
                          body=message
                          )
    print("[x] Sent %r"%message)
    connection.close()
    

     消费者:fanout_consumer.py

    import pika
    import sys
    connection = pika.BlockingConnection(pika.ConnectionParameters("localhost"))
    
    channel = connection.channel()
    
    channel.exchange_declare(exchange='logs',
                             exchange_type='fanout')
    #exclusive 不指定queue名字 ,rabbitmq会随机分一个,exclusive会在使用此queue的消费者
    #断开后,自动将queue删除
    result =channel.queue_declare(exclusive=True)
    queue_name=result.method.queue#取到queue名字
    print("random queuename",queue_name)
    #绑定转发器,让发送端知道是哪个queue
    channel.queue_bind(exchange='logs',
                       queue=queue_name)
    print(' [*] Waiting for logs, To exit press CRTL +C')
    
    def callback(ch,method,properties,body):
        print("[x] %r" % body)
    
    
    channel.basic_consume(callback,
                          queue=queue_name,
                          no_ack=True)#不确认消息
    channel.start_consuming()
    
  • 相关阅读:
    线阵相机、镜头及光源的选型
    查看mysql数据库容量大小
    mysql 表分区操作
    VC++ 全局变量定义
    sql按半小时统计
    sqlserver/mysql按天、按小时、按分钟统计连续时间段数据
    相机光学 接圈
    Excel中如何锁定部分单元格内容不被修改?
    VC++ 捕获不了异常
    mysql 数据库存储路径更改
  • 原文地址:https://www.cnblogs.com/fuyuteng/p/9254013.html
Copyright © 2011-2022 走看看