zoukankan      html  css  js  c++  java
  • 5.rabbitmq 主题

    1.生产者

    #!/usr/bin/env python
    import pika
    import sys
    
    connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
    channel = connection.channel()
    
    channel.exchange_declare(exchange='topic_logs',
                             exchange_type='topic')
    
    routing_key = sys.argv[1] if len(sys.argv) > 2 else 'anonymous.info'
    message = ' '.join(sys.argv[2:]) or 'Hello World!'
    channel.basic_publish(exchange='topic_logs',
                          routing_key=routing_key,
                          body=message)
    print(" [x] Sent %r:%r" % (routing_key, message))
    connection.close()

    2. 消费者

    import pika
    import sys
    
    connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
    channel = connection.channel()
    
    channel.exchange_declare(exchange='topic_logs',
                             exchange_type='topic')
    
    result = channel.queue_declare(exclusive=True)
    queue_name = result.method.queue
    
    binding_keys = sys.argv[1:]
    if not binding_keys:
        sys.stderr.write("Usage: %s [binding_key]...
    " % sys.argv[0])
        sys.exit(1)
    
    for binding_key in binding_keys:
        channel.queue_bind(exchange='topic_logs',
                           queue=queue_name,
                           routing_key=binding_key)
    
    print(' [*] Waiting for logs. To exit press CTRL+C')
    
    def callback(ch, method, properties, body):
        print(" [x] %r:%r" % (method.routing_key, body))
    
    channel.basic_consume(callback,
                          queue=queue_name,
                          no_ack=True)
    
    channel.start_consuming()

    3.执行:

    消费者:python rec.py kern.*                   python rec.py #

    生产者:python  work.py kern. info

    *(星号)可以替代一个单词。
    #(hash)可以替换零个或多个单词。
  • 相关阅读:
    bzoj 2744 朋友圈
    bzoj 3674 可持久化并查集加强版
    3.15 模拟赛
    luogu 4720 【模板】扩展卢卡斯
    洛谷 P1447 [NOI2010]能量采集 (莫比乌斯反演)
    洛谷P2522 [HAOI2011]Problem b (莫比乌斯反演+容斥)
    7-12 与零交换 (25 分)
    理想的正方形(单调队列)
    Flowerpot(单调队列)
    生日礼物(单调队列)
  • 原文地址:https://www.cnblogs.com/myvic/p/9686076.html
Copyright © 2011-2022 走看看