• rabbitmq安装使用


    使用 http://www.open-open.com/lib/view/open1325131828249.html

    ubuntu:
    apt-get install erlang-nox
    sudo apt-get install rabbitmq-server
    启动
    /etc/init.d/rabbitmq-server start|stop|restart (模式)

    创建目录
    sudo rabbitmqctl add_vhost /pyhtest
    创建用户名
    sudo rabbitmqctl add_user pyh pyh1234
    设置用户权限
    sudo rabbitmqctl set_permissions -p /pyhtest pyh “.*” “.*” “.*”

    启动 sudo  ./bin/rabbitmq-server

    python 调用
    下载py-amqplib https://pypi.python.orgpypi?:action=display&name=amqplib
    sudo chmod 777 setup.py
    安装
    python setup.py install

    生产者

    from amqplib import client_0_8 as amqp
    import sys
    
    filelist=[]
    
    def readfiletolist(filename):
        file = open(filename)
        for line in file.readlines():
         line=line.strip('
    ')
         filelist.append(line)
    #     print "line" + line
    
    
    conn = amqp.Connection(host="localhost:5672", userid="guest", password="guest", virtual_host="/", insist=False)
    chan = conn.channel()
    
    #readfiletolist("1.txt")
    readfiletolist("/home/tanbo/stockdata/2014-06-27.txt")
    for i in filelist:
            #msg = amqp.Message(i)
    #msg = amsg = amqp.Message(sys.argv[1])
            msg = amqp.Message(i)
            msg.properties["delivery_mode"] = 2
            chan.basic_publish(msg,exchange="sorting_room",routing_key="jason")
    
    chan.close() 
    conn.close()
    

    消费者

    from amqplib import client_0_8 as amqp
    
    conn = amqp.Connection(host="localhost:5672", userid="guest", password="guest", virtual_host="/", insist=False)
    chan = conn.channel()
    
    chan.queue_declare(queue="po_box", durable=True, exclusive=False, auto_delete=False)
    chan.exchange_declare(exchange="sorting_room", type="direct", durable=True, auto_delete=False,)
    
    chan.queue_bind(queue="po_box", exchange="sorting_room", routing_key="jason")
    
    def recv_callback(msg):
        print 'Received: ' + msg.body + ' from channel #' + str(msg.channel.channel_id)
    
    chan.basic_consume(queue='po_box', no_ack=True, callback=recv_callback, consumer_tag="testtag")
    while True:
        chan.wait()
    chan.basic_cancel("testtag")
    
    
    chan.close()
    conn.close()
    
  • 相关阅读:
    有趣的F-String
    停止使用非版本控制的可执行代码
    Django ORM中,如何使用Count来关联对象的子集数量
    Django Tastypie: 贴士,技巧和故障排除
    我实在不懂Python的Asyncio
    使用Let's Encrypt为网站加入SSL证书
    [debug]记一次竞态更新bug的解决
    我的web聊天之---序章
    我的音乐盒子(nodejs7 + koa2 + vue + vuex + vue-router)
    装饰器 生成器 进阶
  • 原文地址:https://www.cnblogs.com/chengxin1982/p/3820759.html
Copyright © 2020-2023  润新知