zoukankan      html  css  js  c++  java
  • python rabbitmq官方文档demo

    1.生产者

    #!/usr/bin/env python
    import pika
    import json
    
    # https://www.rabbitmq.com/tutorials/tutorial-one-python.html 官方文档
    
    connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
    channel = connection.channel()
    channel.queue_declare(queue='hello')
    
    message=json.dumps({'OrderId':"1000"})
    channel.basic_publish(exchange='',
                          routing_key='hello',
                          body=message)
    #print(" [x] Sent 'Hello World! 2020'")
    print(message)
    connection.close()

    2.消费者

    #!/usr/bin/env python
    import pika, sys, os
    
    # https://www.rabbitmq.com/tutorials/tutorial-one-python.html 官方文档
    
    def main():
        connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
        channel = connection.channel()
        channel.queue_declare(queue='hello')
    
        def callback(ch, method, properties, body):
            #print(" [x] Received [%r]" %body)
            #print(body)    # b'{"OrderId": "1000"}'
            print(body.decode())  ## 关键:需要decode,否则会出现上面的b' '符号
    
        channel.basic_consume(queue='hello',
                              auto_ack=True,
                              on_message_callback=callback)
    
        print(' [*] Waiting for messages. To exit press CTRL+C')
        channel.start_consuming()
    
    
    if __name__ == '__main__':
        try:
            main()
        except KeyboardInterrupt:
            print('Interrupted')
            try:
                sys.exit(0)
            except SystemExit:
                os._exit(0)
  • 相关阅读:
    百度缓存
    thrift文件编写
    thrift初窥
    Shutil模块介绍
    多线程访问网站的爬虫的问题
    点滴记录python linux中的个人经验
    Yii框架的form处理
    使scrapy支持ftp下载
    mojoportal学习——文章翻译之站点图标
    mojoportal学习之特色模块对Artisteer2.4生成的模板的支持
  • 原文地址:https://www.cnblogs.com/oktokeep/p/14033936.html
Copyright © 2011-2022 走看看